当前位置:   article > 正文

Python 操作SQlite库_python int too large to convert to sqlite integer

python int too large to convert to sqlite integer


(1)创建数据库

 

 

  1. import sqlite3
  2. # test.db is a file in the working directory.
  3. conn = sqlite3.connect("test.db")
  4. c = conn.cursor()
  5. # create tables
  6. c.execute('''CREATE TABLE category
  7. (id int primary key, sort int, name text)''')
  8. c.execute('''CREATE TABLE book
  9. (id int primary key,
  10. sort int,
  11. name text,
  12. price real,
  13. category int,
  14. FOREIGN KEY (category) REFERENCES category(id))''')
  15. # save the changes
  16. conn.commit()
  17. # close the connection with the database
  18. conn.close()
'
运行

 

 

 

 

(2)插入数据

 

  1. import sqlite3
  2. conn = sqlite3.connect("test.db")
  3. c = conn.cursor()
  4. books = [(1, 1, 'Cook Recipe', 3.12, 1),
  5. (2, 3, 'Python Intro', 17.5, 2),
  6. (3, 2, 'OS Intro', 13.6, 2),
  7. ]
  8. # execute "INSERT"
  9. c.execute("INSERT INTO category VALUES (1, 1, 'kitchen')")
  10. # using the placeholder
  11. c.execute("INSERT INTO category VALUES (?, ?, ?)", [(2, 2, 'computer')])
  12. # execute multiple commands
  13. c.executemany('INSERT INTO book VALUES (?, ?, ?, ?, ?)', books)
  14. conn.commit()
  15. conn.close()

 

 

 

 

 

(3)查询数据

 

  1. import sqlite3
  2. conn = sqlite3.connect('test.db')
  3. c = conn.cursor()
  4. # retrieve one record
  5. c.execute('SELECT name FROM category ORDER BY sort')
  6. print(c.fetchone())
  7. print(c.fetchone())
  8. # retrieve all records as a list
  9. c.execute('SELECT * FROM book WHERE book.category=1')
  10. print(c.fetchall())
  11. # iterate through the records
  12. for row in c.execute('SELECT name, price FROM book ORDER BY sort'):
  13. print(row)
print(row)

 

 

 

 

 

(4)更新与删除

 

  1. conn = sqlite3.connect("test.db")
  2. c = conn.cursor()
  3. c.execute('UPDATE book SET price=? WHERE id=?',(1000, 1))
  4. c.execute('DELETE FROM book WHERE id=2')
  5. conn.commit()
  6. conn.close()

 

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号