这篇文章主要讲解了“怎么用python操作sqlite3”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用python操作sqlite3”吧!
代码如下:
import sqlite3 con = sqlite3.connect('./db/user.db') cur = con.cursor() sql = 'create table if not exists user(id integer primary key, name text, age integer)' cur.execute(sql) # 插入一条 or 多条 def create_user(): # insert # cur.execute('insert into user values(?,?,?)', (1, 'adleytales', 3)) # insert manay list = [ (2, 'para', 13), (3, 'aliya', 16), (4, 'balan', 12), (5, 'okok', 10), ] cur.executemany('insert into user values(?,?,?)', list) # 查询 def select_users(): cur.execute('select * from user') result = cur.fetchall() # [(1, 'adleytales', 3), (2, 'para', 13), (3, 'aliya', 16), (4, 'balan', 12), (5, 'okok', 10)] print(result) def select_users2(): cur.execute('select * from user') result = cur.fetchone() print(result) # (1, 'adleytales', 3) def select_users3(): cur.execute('select * from user') result = cur.fetchmany(2) print(result) # [(1, 'adleytales', 3), (2, 'para', 13)] # 更新 def update_user(): cur.execute('update user set name=? where id =?', ('new_adleytales', 5)) # 删除 def delete_user(): cur.execute('delete from user where id=?', (3,)) # create_user() # select_users() # select_users2() select_users3() # update_user() # delete_user() con.commit() con.close()
感谢各位的阅读,以上就是“怎么用python操作sqlite3”的内容了,经过本文的学习后,相信大家对怎么用python操作sqlite3这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。