Я пытаюсь вставить значения в мою таблицу sqlite, используя скрипт на python.
Это работало отлично, пока я не попытался добавить еще один столбец под названием «информация» - он затем выдал следующую ошибку:
You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings
Итак, я добавил:
conn.text_factory = str
Тогда я получил эту ошибку:
Incorrect number of bindings supplied. The current statement uses 7, and there are 3 supplied.
Мне кажется, проблема в том, что в этом новом столбце «информация» содержится несколько строк текста, поэтому я могу неправильно указать его как «текст». Код моего скрипта Python:
import sqlite3;
from datetime import datetime, date;
import time
conn = sqlite3.connect('mynewtable.sqlite3')
conn.text_factory = str
c = conn.cursor()
c.execute('drop table if exists mynewtable')
c.execute('create table mynewtable(id integer primary key autoincrement, rank integer, placename text, information text, nooftimes integer, visit text, fav integer, year integer)')
def mysplit (string):
quote = False
retval = []
current = ""
for char in string:
if char == '"':
quote = not quote
elif char == ',' and not quote:
retval.append(current)
current = ""
else:
current += char
retval.append(current)
return retval
# Read lines from file, skipping first line
data = open("mynewtable.csv", "r").readlines()[1:]
for entry in data:
# Parse values
vals = mysplit(entry.strip())
# Insert the row!
print "Inserting %s..." % (vals[0])
sql = "insert into mynewtable values(NULL, ?, ?, ?, ?, ?, ?, ?)"
c.execute(sql, vals)
# Done!
conn.commit()