With the continuous development of information technology, various SQL injection attack tools emerge one after another, and the types of attacks are varied. SQL injection has always been the main problem of network security. Therefore, this paper proposes an intrusion detection method based on N-Gram and tfidf(term frequency inverse document frequency). The core idea is using N-Gram to select feature words in the preprocessing stage and TFIDF to vectorize SQL sentences. Then on the basis of this data set the SVM classifier is trained. Finally, the classification effect is tested by comparing with existing research. Experimental results show that, compared with using predefined feature vectors directly, this method can improve the accuracy on the basis of ensuring the recall rate.