使用连续添加的列将数据添加到数据库

时间:2022-03-13 08:04:05

Hello I am trying to add data to a database with sqlite3 in python. However, I am not so sure on how to write the sql code to add data to a database that continuously gets more columns. How would I write the sql code to add data to database that continuously gets more columns.

您好我正在尝试使用python中的sqlite3向数据库添加数据。但是,我不太确定如何编写sql代码来将数据添加到不断获取更多列的数据库中。如何编写sql代码以将数据添加到不断获取更多列的数据库中。

thank you for your time

感谢您的时间

1 个解决方案

#1


0  

To insert data you may use the cursor to execute the query. See the example from python tutorial http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php

要插入数据,您可以使用光标执行查询。请参阅python教程中的示例http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php

 db.close()
 import sqlite3
 db = sqlite3.connect('data/test.db')
 cursor = db.cursor()
 cursor.execute('''CREATE TABLE books(id INTEGER PRIMARY KEY,
...                    title TEXT, author TEXT, price TEXT, year TEXT)
...                ''')
 db.commit()

 import sqlite3
 db = sqlite3.connect('data/test.db')
 cursor = db.cursor()
 title1 = 'Learning Python'
author1 = 'Mark Lutz'
price1 = '$36.19'
year1 ='Jul 6, 2013'

title2 = 'Two Scoops of Django: Best Practices For Django 1.6'
author2 = 'Daniel Greenfeld'
price2 = '$34.68'
year2 = 'Feb 1, 2014'

cursor.execute('''INSERT INTO books(title, author, price, year)
...                   VALUES(?,?,?,?)''', (title1, author1, price1, year1))

cursor.execute('''INSERT INTO books(title, author, price, year)
...                   VALUES(?,?,?,?)''', (title2, author2, price2, year2))

db.commit()

Maybe this would help.

也许这会有所帮助。

#1


0  

To insert data you may use the cursor to execute the query. See the example from python tutorial http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php

要插入数据,您可以使用光标执行查询。请参阅python教程中的示例http://www.bogotobogo.com/python/python_sqlite_connect_create_drop_table.php

 db.close()
 import sqlite3
 db = sqlite3.connect('data/test.db')
 cursor = db.cursor()
 cursor.execute('''CREATE TABLE books(id INTEGER PRIMARY KEY,
...                    title TEXT, author TEXT, price TEXT, year TEXT)
...                ''')
 db.commit()

 import sqlite3
 db = sqlite3.connect('data/test.db')
 cursor = db.cursor()
 title1 = 'Learning Python'
author1 = 'Mark Lutz'
price1 = '$36.19'
year1 ='Jul 6, 2013'

title2 = 'Two Scoops of Django: Best Practices For Django 1.6'
author2 = 'Daniel Greenfeld'
price2 = '$34.68'
year2 = 'Feb 1, 2014'

cursor.execute('''INSERT INTO books(title, author, price, year)
...                   VALUES(?,?,?,?)''', (title1, author1, price1, year1))

cursor.execute('''INSERT INTO books(title, author, price, year)
...                   VALUES(?,?,?,?)''', (title2, author2, price2, year2))

db.commit()

Maybe this would help.

也许这会有所帮助。