SQLite 创建数据库
sqlite3 drugbank.db #创建drugbank.db数据库 .open drugbank.db #打开drugbank.db数据库
通过Python3操作数据库
import pandas import csv, sqlite3
连接数据库
conn= sqlite3.connect("drugbank.db")
csv数据导入drugbank表
df = pandas.read_csv('drugbank.csv') df.to_sql('drugbank', conn, if_exists='append', index=False)
创建一个Cursor:
cursor = conn.cursor()
查看表名
cursor.execute("select name from sqlite_master where type='table' order by name") print (cursor.fetchall())
[('drugbank',)][('drugbank',)]
获得查询结果表的列名
cursor.execute("select * from drugbank") col_name_list = [tuple[0] for tuple in cursor.description] print (col_name_list)
查询
# 执行查询语句: cursor.execute('select smiles from drugbank ') # 获得查询结果集: values = cursor.fetchall() # 遍历打印输出 for smile in values: print(smile)