manxing
manxing

Reputation: 3305

insert query mysql and python

def get(infilename):

    fd_in = open(infilename, "r")

    try:
         con = mdb.connect (host=MY_HOST, user=MY_USER, passwd=MY_PASS, db=MY_DB)
         cur = con.cursor()
         insertQuery = "LOAD DATA LOCAL INFILE" + infilename + "INTO TABLE vlan_area (vlan_id, area)" 

In python, I want to import a txt file to Mysql database, I want to get the file name dynamically, but this inserQuery is wrong, I don't know how to write it, can anyone help? many thanks!

Upvotes: 0

Views: 314

Answers (2)

Rahul
Rahul

Reputation: 77856

should be like this:

insertQuery = "LOAD DATA LOCAL INFILE " + infilename + " INTO TABLE vlan_area 
(vlan_id, area)"   

Upvotes: 1

Kien Truong
Kien Truong

Reputation: 11381

You forgot the space after INFILE and before INTO. Also make sure to sanitize infilename or risk SQL injections.

Upvotes: 3

Related Questions