[问题] SQLite insert 问题

楼主: alansyue (alansyue)   2018-06-23 14:36:15
大家好,小弟是 Python 新手,非本科系自学
最近在学习将 Python 的资料汇入 SQLite
我先建一个SQLite的资料表
import sqlite3
conn = sqlite3.connect('sqlite01.db')
print("Opened database successfully")
c = conn.cursor()
c.execute('''CREATE TABLE worknowapp_job
(ID INT PRIMARY KEY NOT NULL,
JOB_NAME CHAR NOT NULL,
JOB_URL CHAR NOT NULL);''')
print("Table created successfully")
conn.commit()
conn.close()
接着我想试将设定好的变量写入 SQLite,程式码如下:
import sqlite3
name ="测试1"
url = "/jobs/10101"
conn = sqlite3.connect('sqlite01.db')
c = conn.cursor()
print ("Opened database successfully")
c.execute("INSERT INTO worknowapp_job (ID,JOB_NAME,JOB_URL) \
VALUES (1,name,url )");
conn.commit()
print("Records created successfully")
conn.close()
但却出现以下的错误讯息:
作者: s8300355 (s8300355)   2018-06-23 15:38:00
c.execute("INSERT INTO worknowapp_job (ID,JOB_NAME,J执行的地方改成这样应该可以VALUES (%d,'%s','%s')" % (1, name, url)或者这样 VALUES (?, ?, ?)" , (2, name, url)
作者: MAGICXX (逢甲阿法)   2018-06-23 16:15:00
推荐用%s ?很容易爆炸
楼主: alansyue (alansyue)   2018-06-24 01:20:00
成功了,谢谢楼上两位大大

Links booklink

Contact Us: admin [ a t ] ucptt.com