如何将数组插入sqlite数据库

时间:2019-08-04 14:50:23

标签: python database sqlite

我有这个数组

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中做到这一点,请帮帮我。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您是否已经创建了数据库和表来存储此数据?