含数据库连接、表创建、增删改查。查看sqlite数据库的软件推荐使用sqlitestudio,下载地址:sqlitestudio-SQLite文档类资源-CSDN下载。
# -*- coding: utf-8 -*-
"""
乐乐感知学堂公众号
@author: https://blog.csdn.net/suiyingy
"""
import sqlite3
class conndb():
list1=[]
def __init__(self):
self.conn = sqlite3.connect('testqqs.db') #存在就连接,不存在就创建数据库
self.c = self.conn.cursor() #获取操作数据库的游标
def query_sql(self,sql): #执行select并返回数据的方法
result=self.c.execute(sql)
print(result)
for res in result:
print(res)
self.list1.append(res[0])
print(list(set(self.list1)))
self.conn.commit()
# self.conn.close()
def execute_sql(self,sql): #执行除select外语句的方法
result=self.c.execute(sql)
print("the result is:",result)
if "create table" in str(sql).lower():
print("Table created successfully")
else:
print("the createdb.sql execute is successfully")
self.conn.commit()
# self.conn.close()
if __name__=="__main__":
table1='''
create TABLE IF NOT EXISTS qq_infos(
id INTEGER PRIMARY KEY AUTOINCREMENT,
`qq_number` varchar(255) DEFAULT NULL,
`qq_sex` varchar(255) DEFAULT NULL,
`qq_years` varchar(255) DEFAULT NULL,
`qq_injoin_time` varchar(255) DEFAULT NULL,
`qq_say_time` varchar(255) DEFAULT NULL
);
'''
insert_sql="INSERT INTO qq_infos (id,qq_number,qq_sex,qq_years,qq_injoin_time,qq_say_time) VALUES (1,'xxxx,'x','xxxx','2015/04/21','xxx')"
query_sql="select * from qq_infos"
delete_sql="delete from qq_infos where id=765"
update_sql="update qq_infos set qq_number='625888888' where id=1"
db=conndb()
sql="select qq_number from table0607 where id>4366"
db.execute_sql(table1)
db.query_sql(query_sql)