You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.0 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""
数据库交互模块
凡是操作数据库的函数均写在该模块
"""
import sqlite3
# 构建一个名为conn的连接对象实现python程序和SOLite数据库unman_vehicle.db之间的连接
conn = sqlite3.connect('turndb.db', check_same_thread=False)
# 创建游标对象,为了进一步操作
cur = conn.cursor()
# 外键默认是关闭,若要使用则要设置为打开
cur.execute("PRAGMA foreign_keys = ON")
# 返回数据库句柄,可以采用该句柄进行数据库操作
def get_cur():
return cur
# 构建数据表创建及文本数据导入函数
def create_table(tab_name, col_prop_list, txt_path, conn=conn, cur=cur):
# 以,为分隔符将col_prop_list中的元素连接起来
col_name_props = ','.join(col_prop_list)
# 如果这个表不存在,就创建这个表,括号内是列名
cur.execute('CREATE TABLE IF NOT EXISTS %s( %s)' % (tab_name, col_name_props))
# 以只读的方式打开 txt_path,返回一个文件描述符
f = open(txt_path, 'r')
for x in f:
# 以为分隔符分割x中的字符并删除分割好字符右边的空白字符
x = x.rstrip().split(',')
# len(x)返回得到x的长度
a = ["'%s'" % x[i] for i in range(len(x))]
# 以为分隔符将a中的元素连起来
x = ','.join(a)
# 插入数据
cur.execute('INSERT INTO %s VALUES(%s)' % (tab_name, x))
f.close()
print('%s 创建成功' % tab_name)
print('%s 导入成功' % txt_path)
# 提交事物执行
conn.commit()
# 构建数据表创建及文本数据导入函数
def create_table_without_data(tab_name, col_prop_list, conn=conn, cur=cur):
# 以,为分隔符将col_prop_list中的元素连接起来
col_name_props = ','.join(col_prop_list)
# 如果这个表不存在,就创建这个表,括号内是列名
cur.execute('CREATE TABLE IF NOT EXISTS %s( %s)' % (tab_name, col_name_props))
print('%s 创建成功' % tab_name)
# 提交事物执行
conn.commit()
# 构建数据表结构查询函数
def table_struct(tab_name, cur=cur):
cur.execute("PRAGMA table_info(%s)" % tab_name)
# 返回多个元组如果没有就返回NOne
t_struct = cur.fetchall()
# 在返回的元组的遍历
for item in t_struct:
for x in item:
# 把 x 转换string
x = str(x)
# 多值打印是 以制表符为分割为end添加一个空字符不换行
print(x, sep='\t', end=' ')
print()
# 构建数据表内容查询函数
def table_quer(tab_name='turndb', col_names='*', num_line=None, cur=cur):
cur.execute('select %s from %s where id=%s' % (col_names, tab_name, 1))
Li = cur.fetchall()
"""for line in Li[:num_line]:
for item in line:
print(item, sep='\t', end=' ')
print()"""
return Li
# 更新数据
def update(tab_name='fit_param_Qudong', col_names='*', num_line=None, cur=cur, x=None):
cur.execute('UPDATE %s SET %s = %s WHERE ID = %s' % (tab_name,col_names,x,1))
Li = cur.fetchall()
return Li
# 获取一定行数的数据
def table_quer_limit(tab_name, col_names='*', num_line=20, cur=cur):
sql = 'select %s from %s order by id desc limit 0,%s' % (col_names, tab_name, num_line)
cur.execute(sql)
Li = cur.fetchall()
return Li
# 添加一列默认添加类型为float
def addcol(tab_name, col_name='*', conn=conn, cur=cur):
cur.execute("ALTER TABLE %s ADD COLUMN '%s' float" % (tab_name, col_name))
conn.commit()
# 更新数据
# 插入数据
def insert_data(tab_name, data):
cur.execute('INSERT INTO %s VALUES(%s)' % (tab_name, data))
conn.commit()
# 清空表格 @tab_name 表格名称
def clear_table(tab_name):
cur.execute('delete from %s ' % tab_name)
conn.commit()
# 关闭数据库句柄,要在整个程序运行结束的时候调用该方法
def close():
conn.close()
if __name__ == '__main__':
pass
tab_name_3 = 'turndb'
col_prop_list_3 = ['id','angle',]
create_table_without_data(tab_name_3, col_prop_list_3)