如何使用Python将数据插入SQLite3数据库?
我需要使用Python将数据插入SQLite3数据库。我已经编写了查询,但它没有像我预期的那样工作。我的代码如下如何使用Python将数据插入SQLite3数据库?,python,sqlite,Python,Sqlite,我需要使用Python将数据插入SQLite3数据库。我已经编写了查询,但它没有像我预期的那样工作。我的代码如下 conn = sqlite3.connect("db.sqlite3") cursor = conn.cursor() location_name = request.POST.get('lname') rname = request.POST.get('rname') seat = request.POST.get('seat') projector = request.POST.
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
location_name = request.POST.get('lname')
rname = request.POST.get('rname')
seat = request.POST.get('seat')
projector = request.POST.get('projector')
video = request.POST.get('video')
location_name = location_name[0:255]
rname = rname[0:255]
seat = seat[0:10]
from_date = request.POST.get('from_date')
to_date = request.POST.get('from_date')
current_datetime = datetime.datetime.now()
now = current_datetime.strftime("%Y-%m-%d %H:%M")
cursor.execute("INSERT INTO booking_meeting (room_name,from_date,to_date,no_seat,projector,video,created_date,location_name) \ VALUES (rname, from_date, to_date, seat, projector, video, now, location_name )")
conn.commit()
这里我给出的是动态值,没有数据插入到表中。您需要将变量的值放入SQL语句中。最安全的方法是使用以下方法
cursor.execute("INSERT INTO booking_meeting (room_name,from_date,to_date,no_seat,projector,video,created_date,location_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (rname, from_date, to_date, seat, projector, video, now, location_name ))
请注意,变量作为元组传递,以便在SQL语句中使用它们的值。您需要将变量的值放入SQL语句中。最安全的方法是使用以下方法
cursor.execute("INSERT INTO booking_meeting (room_name,from_date,to_date,no_seat,projector,video,created_date,location_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (rname, from_date, to_date, seat, projector, video, now, location_name ))
请注意,变量作为元组传递,以便在SQL语句中使用它们的值。除了@Code peedient: 您可以使用
executemany
插入许多值:
cursor.executemany(
"INSERT INTO booking_meeting (room_name,from_date,to_date,no_seat,projector,video,created_date,location_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[
(rname1, from_date1, to_date1, seat1, projector1, video1, now1, location_name1),
(rname2, from_date2, to_date2, seat2, projector2, video2, now2, location_name2)
]
)
除了@Code学徒: 您可以使用
executemany
插入许多值:
cursor.executemany(
"INSERT INTO booking_meeting (room_name,from_date,to_date,no_seat,projector,video,created_date,location_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[
(rname1, from_date1, to_date1, seat1, projector1, video1, now1, location_name1),
(rname2, from_date2, to_date2, seat2, projector2, video2, now2, location_name2)
]
)
您是否尝试过在我写的结尾处执行
cursor.commit()
。我认为查询中有任何错误。什么是conn
?请尝试此光标。执行(“插入到预定会议(房间名称,从日期,到日期,无座位,投影仪,视频,创建日期,位置名称)\VALUES”%(rname,从日期,到日期,座位,投影仪,视频,现在,位置名称))
@ksai SQL语句的字符串没有格式说明符来放置元组中的数据。您是否尝试了cursor.commit()。我认为查询中有任何错误。什么是conn
?请尝试此光标。执行(“插入到预定会议(房间名称,从日期,到日期,无座位,投影仪,视频,创建日期,位置名称)\VALUES”%(rname,从日期,到日期,座位,投影仪,视频,现在,位置名称))
@ksai SQL语句的字符串没有用于放置元组数据的格式说明符。是的,看起来他正在填充所有表,所以:cursor.execute(“插入到预定会议值(?,,,,,,,,,,,,,,,,?),(rname,from,to,date,seat,projector,video,now,location,name))应该足够了,对吗?是的,看起来他正在填充所有的表格,所以:cursor.execute(“插入到预定会议值中(?,,,,,,,,,,,,,,,,,?),(rname,from,to,date,seat,projector,video,now,location,name))应该足够了,对吗?