gpt4 book ai didi

python - 如何在 python 中创建一个包含 1 行的 sqlite3 数据库

转载 作者:行者123 更新时间:2023-12-01 09:30:57 25 4
gpt4 key购买 nike

我想知道是否有办法在 python 中使用单行(记录)数据创建 sqlite3 数据库,并且当我再次执行代码时,它不会被创建两次。

到目前为止,我已经尝试过编码:

conn = sqlite3.connect('db_M.db')
c = conn.cursor()
no1 = "Null"
no2 = "Null"
no3 = "Null"
value = 1
try:
c.execute("CREATE TABLE IF NOT EXISTS M_Memory(Name TEXT, Person TEXT, memory TEXT, value REAL)")
if True:
c.execute("INSERT INTO M_Memory(Name, Person, memory, value) VALUES (?, ?, ?, ?)",
(no1, no2, no3, value))
except:
print(" foo ")
conn.commit()

最佳答案

我解决了我自己的问题..如果您有更好的代码请分享..谢谢大家

import sqlite3


conn = sqlite3.connect('db_M.db')
c = conn.cursor()

c.execute("CREATE TABLE IF NOT EXISTS M_Memory(Name TEXT, Person TEXT, memory TEXT, value REAL)")


text10 = input("what do you want to call the memory ?\n")

text11 = input("what or who the memory will be about?\n")

text12 = input("what do you want me to save in it ?\n")


def Get_The_Max_Function():
Max_Value = []
c.execute('SELECT MAX(value) FROM M_Memory')
lolo = c.fetchone()
if lolo[0] is None:
lolo = 0
Max_Value.insert(0, lolo)
else:
x = int((lolo[0]))
Max_Value.insert(0, x)
return Max_Value


def dynamic_data_entry(Max_Value):
Name = text10
Person = text11
memory = text12
value = Max_Value[0] + 1

c.execute("INSERT INTO M_Memory(Name, Person, memory, value) VALUES (?, ?, ?, ?)",
(Name, Person, memory, value))
conn.commit()


dynamic_data_entry(Get_The_Max_Function())
c.close()
conn.close()

关于python - 如何在 python 中创建一个包含 1 行的 sqlite3 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49966061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com