我想要一些建议,为什么这不会将数据插入我的SQL表

时间:2011-05-27 05:44:32

标签: python sql text-files

以下是我的代码:

import MySQLdb
def insert_popularity(PersonNumber, Category, Value):

# make a connection to the dataabse
connection = MySQLdb.connect(host='localhost', user='root', \
                             passwd='password', db='inb104')

# get a cursor on the database
cursor = connection.cursor()

# construct the SQL statement
sql = ("""INSERT INTO popularity (PersonNumber, Category, Value)
        VALUES(%s, %s, %s)""", (number, category, data))

def open_file(filename):        
    txt_file = file(filename, 'r')
    for line in txt_file:
        # Split the line on whitespace
        for value in line.split():                
            return value

            number = value[0]
            data = value[1]

# execute the query
cursor.execute(sql)

# commit the changes to the database\
connection.commit()

# close the cursor and connection
cursor.close()

connection.close()

更新

根据Paulo's suggestion更改我的代码后,我现在收到此错误:

query() argument 1 must be string or read-only buffer, not tuple. 

我不确定在尝试更改代码后它是什么:

def insert_popularity(Category, filename, cursor):

    txt_file = file(filename, 'r')
    for line in txt_file:
            # Split the line on whitespace
        number, value = line.split()
            # construct the SQL statement
        sql = ("""INSERT INTO popularity (PersonNumber, Category, Value)
                VALUES(%s, %s, %s)""", (number, Category, value))
            # execute the query
        cursor.execute(sql)

connection = MySQLdb.connect(host='localhost', user='root', \
                                 passwd='password', db='dogs')

cursor = connection.cursor()

Category = 'dogs'
insert_popularity(Category, 'dogs.txt', cursor)
connection.commit()
cursor.close()
connection.close()

5 个答案:

答案 0 :(得分:2)

您已创建要作为元组执行的查询。有两种可能性来解决这个问题:

  1. 使用创建的查询(sql)作为参数列表:

    sql = ("""INSERT INTO popularity (PersonNumber, Category, Value) VALUES(%s, %s, %s)""", (number, Category, value)) # execute the query cursor.execute(*sql)

  2. 直接将查询添加到execute方法:

    cursor.execute("""INSERT INTO popularity (PersonNumber, Category, Value) VALUES(%s, %s, %s)""", (number, Category, value))

  3. 2号绝对是比第一个更好的选择。感谢所有评论!

答案 1 :(得分:2)

只是简单地做,一次一件事,没有任何花哨的东西容易出错,并且在他们浏览混淆时减慢读者的速度:

sql = """INSERT INTO popularity (PersonNumber, Category, Value) VALUES (%s, %s, %s)"""
args = (number, Category, value)
cursor.execute(sql, args)

你的评论(执行查询)消失了,因为(a)它是错误的(插入!=查询)和(b)固定版本(执行插入)将是非常多余的,因为固定代码的清晰度。 / p> 新问题解决后,

更新(解压缩的值太多):

而不是这段代码:

for line in txt_file:
        # Split the line on whitespace
    number, value = line.split()

这样做:

for lino, line in enumerate(txt_file, 1):
    pieces = line.split()
    if len(pieces) != 2:
        print "Bad data in line %d: %r" % (lino, pieces)
        continue
    number, value = pieces

答案 2 :(得分:1)

数字,类别和数据的数据类型是什么?如果其中任何一个是字符串,那么您应该在查询中用单引号将它们包装起来。

答案 3 :(得分:1)

不要误解我的意思,但代码很乱 -

  1. for循环内部的返回将返回第一行中的第一个拆分字符串。
  2. 定义了open_file但从未调用过
  3. 依旧......

    我的意思是:

    def process_file(category, filename, cursor):
        txt_file = file(filename, 'r')
        for line in txt_file:
            number, value = line.split()
            sql = ("""INSERT INTO popularity (PersonNumber, Category, Value)
                VALUES(%s, %s, %s)""", (number, category, data))
            cursor.execute(sql)
    
    connection = MySQLdb.connect(host='localhost', user='root',
                             passwd='password', db='inb104')
    # get a cursor on the database
    cursor = connection.cursor()
    category = 'foo'
    process_file(category, 'somefile.txt', cursor)
    # commit the changes to the database\
    connection.commit()
    # close the cursor and connection
    cursor.close()
    connection.close()
    

答案 4 :(得分:1)

试试这样:

def insert_popularity(Category, filename, cursor):
    sql = """INSERT INTO popularity (PersonNumber, Category, Value)
           VALUES(%s, %s, %s)"""

    txt_file = file(filename, 'r')
    for line in txt_file:
        # Split the line on whitespace
        number, value = line.split()
        # execute the query
        cursor.execute(sql, (number, Category, value))
    txt_file.close()

connection = MySQLdb.connect(host='localhost', user='root', \
                                 passwd='password', db='dogs')

cursor = connection.cursor()

Category = 'dogs'
insert_popularity(Category, 'dogs.txt', cursor)

connection.commit()
cursor.close()
connection.close()

另请注意:您的代码表明这是一个MySQL数据库;如果它是一个SQLite数据库,就像问题的标题所示,请用'?'替换sql语句中的每个'%s'