我mac系统自带的python2.7 没有sqlite3 库。
查了查资料,只好再安装一个 python3.6 ,使用python3.6 的sqlite3 。
下面是建立一个 sqlite 的连接:
# -*- coding:utf-8 -*-
import sqlite3
sqlite_address = '/Users/caobo/Desktop/短歌行/DuanGeXing/dgx/dgx.db'
def conn_sqlite(sql_query):
try:
conn = sqlite3.connect(sqlite_address)
c = conn.cursor()
cursor = c.execute(sql_query)
result = cursor.fetchall()
#fetchall 是列出所有的数据
#fetchone 是只列出一行数据
return result
except Exception as e:
print(e)
finally:
conn.commit()
conn.close()
写一个测试例子:
res = conn_mysql("select title,author from work where author = '李白' limit 10")
print(res)