-
Notifications
You must be signed in to change notification settings - Fork 2
/
2
36 lines (31 loc) · 1.33 KB
/
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sqlite3
conn = sqlite3.connect('qanda.db')
c= conn.cursor()
def createdb():
c.execute("CREATE TABLE IF NOT EXISTS qanda(askuser TEXT, question TEXT, description TEXT, answers TEXT, useranswers TEXT, sentiment REAL)")
def createQuestionEntry(askingUsr, Question, Description):
c.execute("INSERT INTO qanda(askuser, question, description) VALUES (?,?,?)", (askingUsr, Question, Description))
conn.commit()
def addNewAnswer(ansUser, Question, Answer):
c.execute("SELECT useranswers FROM qanda WHERE question='{}'".format(Question))
a = c.fetchall()
acheck = [(None,) for i in range(len(a))]
if acheck == a:
a = []
a.append(ansUser)
c.execute("""UPDATE qanda SET useranswers = ? WHERE question =?""",(str(a), Question))
theanswers = []
theanswers.append(Answer)
c.execute("""UPDATE qanda SET answers = ? WHERE question = ?""",(str(theanswers), Question))
conn.commit()
else:
#this is not the first answer
import ast
a = ast.literal_eval(a)
a.append(ansUser)
theanswers = c.execute("SELECT answers FROM qanda WHERE question='{}'".format(Question))
theanswers = ast.literal_eval(theanswers)
theanswers.append(Answer)
c.execute("""UPDATE qanda SET answers = ? WHERE question=?""", (str(theanswers), Question))
c.execute("""UPDATE qanda SET useranswers = ? WHERE question =?""",(str(a), Question))
conn.commit()