Reputation: 3
How do I get input from user to create databases in python? These data must stored in test.db file.
Code
import sqlite3
conn = sqlite3.connect('test.db');
print "Opened database successfully";
name=raw_input ("enter your name: ");
salary=raw_input("enter your salary: ");
conn.execute("INSERT INTO employee (NAME,SALARY) \
VALUES ( r"+name+",r "+salary+")");
conn.commit()
Upvotes: 0
Views: 996
Reputation: 1043
Your concatenation is broken here, if salary is an integer you should do something like that:
conn.execute("INSERT INTO employee (NAME,SALARY) \
VALUES ( \"" + name + "\", " + str(salary) + ")");
Basically you need to escape some quotes around strings in your query.
Then to read your table you can do something like that:
for row in conn.execute('SELECT * FROM employee'):
print row
Upvotes: 1