Python - 试图让问答从数据库中提取答案

时间:2018-03-08 10:47:34

标签: python sql

# insertions with SQL

# importing module
import sqlite3

# connecting to the database 
conn = sqlite3.connect("illness.db")

c = conn.cursor()

def create_table():
c.execute("CREATE TABLE IF NOT EXISTS illnesses(illnessID INTAGER PRIMARY KEY NOT NULL, illness VARCHAR(30), illnessinfo VARCHAR(50)") 
c.execute("CREATE TABLE IF NOT EXISTS symptoms(symptomID INTAGER PRIMARY KEY NOT NULL, symptom VARCHAR(50)")

def data_entry():
c.execute("INSERT INTO illnesses VALUES(,'Flu','Influenza - Common Cold.')")
c.execute("INSERT INTO illnesses VALUES(,'Acne','Skin Condition')")
c.execute("INSERT INTO illnesses VALUES(,'Alcohol Misuse','Mega Hangover')")
c.execute("INSERT INTO symptoms VALUES(,'Headache')")
c.execute("INSERT INTO symptoms VALUES(,'Spots')")
c.execute("INSERT INTO symptoms VALUES(,'Breathing problems')")
conn.commit()
c.close()
conn.close()

create_table()
data_entry()

根据标题,我正在为数据库运行(使用LiClipse)然而它只是不起作用而且我不确定为什么(作为注释:我看了看youtube视频尝试对此进行排序,我对python真的很陌生,并且不确定如何真正去做这件事。 除此之外,我甚至不确定使用Liclipse是否是最好的选择。这只是朋友建议的......

1 个答案:

答案 0 :(得分:3)

我认为create_table()data_entry()方法中的查询不正确

我无法弄清楚你是否关闭了光标以及data_entry方法内部或外部的连接,因为使用提供的代码没有正确完成缩进,所以我把它们放在下面,或者你可以关闭它们内部data_entry方法

# insertions with SQL

# importing module
import sqlite3

# connecting to the database 
conn = sqlite3.connect("illness.db")
c = conn.cursor()

def create_table():

    c.execute("CREATE TABLE IF NOT EXISTS illnesses(illnessID INTAGER PRIMARY KEY NOT NULL, illness VARCHAR(30), illnessinfo VARCHAR(50))")
    c.execute("CREATE TABLE IF NOT EXISTS symptoms(symptomID INTAGER PRIMARY KEY NOT NULL, symptom VARCHAR(50))")

def data_entry():

    c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(123,'Flu','Influenza - Common Cold.')")
    c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(1234,'Acne','Skin Condition')")
    c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(345,'Alcohol Misuse','Mega Hangover')")
    c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(567,'Headache')")
    c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(234,'Spots')")
    c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(789,'Breathing problems')")


create_table()
data_entry()

conn.commit()
c.close()
conn.close()

这个也应该有用

def data_entry():

    c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(123,'Flu','Influenza - Common Cold.')")
    c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(1234,'Acne','Skin Condition')")
    c.execute("INSERT INTO illnesses(illnessID, illness , illnessinfo) VALUES(345,'Alcohol Misuse','Mega Hangover')")
    c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(567,'Headache')")
    c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(234,'Spots')")
    c.execute("INSERT INTO symptoms (symptomID,symptom) VALUES(789,'Breathing problems')")
    conn.commit()
    c.close()
    conn.close()