码迷,mamicode.com
首页 > 数据库 > 详细

sqlite 创建、插入、查询

时间:2020-04-14 19:04:13      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:sql   except   append   cursor   exce   ble   pre   time   key   

sqlite 创建、插入、查询

import sqlite3, time
db_pwd="C:\\data"
connect = sqlite3.connect(db_pwd)
cursor = connect.cursor()
try:
	sql = ‘‘‘
	create table test(
		[id]        integer PRIMARY KEY autoincrement,
		[uname]     varchar(100),
		[title]     NONE,
		[timestamp] DATETIME(50),
		[url]     NONE
		);
	‘‘‘
	cursor.execute(sql)
except Exception as e:
	if e == "table test already exists":
		pass

text = [1,2,3]
text.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
sql = ‘‘‘insert into test (uname, title, timestamp, url) VALUES (?,?,?,?)‘‘‘
cursor.execute(sql,text)
connect.commit()

sql = ‘‘‘select * from test‘‘‘
for _ in cursor.execute(sql):
	print(_)
connect.close()

打印结果

<sqlite3.Cursor object at 0x00000000022D1E30>
(1, ‘1‘, 2, 3, ‘2020-04-14 17:49:38‘)

sqlite 创建、插入、查询

标签:sql   except   append   cursor   exce   ble   pre   time   key   

原文地址:https://www.cnblogs.com/BenLam/p/12699702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!