0

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()
2
  • Can you add the error you get here? also, raw_input() is input() in python3 Commented Jan 31, 2017 at 14:24
  • Traceback (most recent call last): File "m1.py", line 13, in <module> VALUES ( r"+name+",r "+salary+")"); sqlite3.OperationalError: near "3432": syntax error Commented Jan 31, 2017 at 14:32

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

you should have a grey tick you can click near its score (zero at the time)
You can do DELETE FROM employee WHERE name = "myname" for example.
name=raw_input ("enter your name: "); conn.execute("DELETE FROM employee WHERE name =\""+ name+"\"); conn.commit()
mitesh@mitesh-Inspiron-N5010:~/Desktop$ python m3.py File "m3.py", line 12 conn.execute("DELETE FROM employee WHERE name =\""+ name+"\"); ^ SyntaxError: EOL while scanning string literal mitesh@mitesh-Inspiron-N5010:~/Desktop$
Seems your line isn't complete, should be: conn.execute("DELETE FROM employee WHERE name =\""+ name+"\");")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.