sqlite3在python中的入门

import sqlite3

def sqlite3_create_table(chart, key_attr, attr):
    # command='create table Book ( id varchar(20) primary key, veges varchar(30))'
    command = 'CREATE TABLE ' + chart + '( ' + key_attr + 'VARCHAR(20) PRIMARY KEY, ' + attr + ' VARCHAR(30))'
    cur.execute(command)
    con.commit()


def sqlite3_insert(chart, attr1, attr2, attr1_value_str, attr2_value_str):  # 暂定两个参数
    # command='Insert into Book ( id, veges) values ( "apple" ,"ba")'
    command = 'Insert INTO ' + chart + '(' + attr1 + ', ' + attr2 + ') Values("' + attr1_value_str + '","' + attr2_value_str + '")'
    print(command)
    cur.execute(command)
    con.commit()


def sqlite3_delete(chart, attr, attr_value_str):  # 暂定一个参数
    # command='delete from Book where id = "apple"'
    command = 'delete from ' + chart + ' where ' + attr + '=' + attr_value_str + '"'
    print(command)
    cur.execute(command)
    con.commit()


def sqlite3_update(chart, new_attr, new_attr_value, old_attr, old_attr_value):  # 暂定根据一个参数改一个参数
    # command="update Book set id ='pair' where id ='apple'"
    command = 'update ' + chart + ' set ' + new_attr + '= "' + new_attr_value + '" where ' + old_attr + '="' + old_attr_value + '"'
    print(command)
    cur.execute(command)
    con.commit()


def sqlite3_find(chart):
    # command='select * from Book'
    command = "select * from " + chart
    print(command)
    cur.execute(command)
    con.commit()


def connect_db3(path=r'd:/sqlite3data/mydatabase.db3'):
    con = sqlite3.connect(path)
    cur = con.cursor()
    return con, cur


def close_db3(con, cur):
    cur.close()
    con.close()


con, cur = connect_db3()
chart = "test"
key_attr = "num"
attr = "sex"
try:
    sqlite3_create_table(chart, key_attr, attr)
except:
    print("faild")

try:
    sqlite3_insert(chart, key_attr, attr, "nihao", "nibuhao")
except:
    print("faild")

cur.execute('SELECT * FROM foo3')
print(cur.fetchall())

close_db3(con, cur)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值