如何将数组插入sqlite数据库

2024-10-04 05:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个阵列

stokmiktarı[ ]= {04.08.2019,"Stok Miktarı",40,50,60}

我想像这样将这个数组插入sqlite数据库。你知道吗

-------------------------------------------------------------
Tarih      |     Isim      |   A Store   |   B Store   |   C Store  |
-------------------------------------------------------------
04.08.2019 |  Stok Miktarı |     40      |      50     |      60    |

用python怎么能做到这一点请帮帮我谢谢。你知道吗


Tags: store数据库sqlite数组帮帮我tarihisimstok
2条回答
import sqlite3

db = sqlite3.connect(':memory:') # creates db in RAM
cursor = db.cursor()
cursor.execute('''
    CREATE TABLE stores_info(id INTEGER PRIMARY KEY, tarih DATE,
                 isim TEXT, a_store TEXT, b_store TEXT, c_store TEXT)
''')
db.commit()

stores = [
    ['04.08.2019', 'Stok Miktarı', 40,50,60],
    ['05.07.2019', 'Stok Miktarı 2', 41,51,61],
    ['06.11.2019', 'Stok Miktarı 3', 40,50,60]
]
cursor.executemany('''
    INSERT INTO stores_info(tarih, isim, a_store, b_store, c_store) VALUES(?,?,?,?,?)
''', stores)
db.commit()

# retrive result
cursor.execute(''' SELECT tarih, isim, a_store, b_store, c_store FROM stores ''')
# cursor.fetchone() # retrieves the first row
result = cursor.fetchall()
for row in result:
    print row[0], row[1], row[2], row[3], row[4]

这是输出。。。你知道吗

# by inserting a_store, b_store & tarih to db
05.07.2019 41 51
04.08.2019 40 50

您已经创建了数据库和表来存储这些数据了吗?你知道吗

相关问题 更多 >