creating database in python
32,087
Solution 1
CREATE DATABASE chom;
this should be run from the MySQL command line client, not from the Python shell.
So, first type mysql -u yourusername -p
from your command line. It will ask you for a password, type it in. Then you will get a prompt like this mysql>
. Here is where you would type that query.
Now, if you want to do this through Python, you can, but it will require some more work.
>>> import MySQLdb as db
>>> con = db.connect(user="foo", passwd="secret")
>>> cur = con.cursor()
>>> cur.execute('CREATE DATABASE chom;')
See this guide for some examples on how to work with Python and MySQL using the standard DB API.
Solution 2
If you want create a database,you can try:
import MySQLdb
db1 = MySQLdb.connect(host="localhost",user="root",passwd="****")
cursor = db1.cursor()
sql = 'CREATE DATABASE chom'
cursor.execute(sql)
Author by
chom
Updated on August 07, 2020Comments
-
chom over 3 years
I have installed mysql for python and running python from command line. I am getting syntax error while creating a database.
>>> import MySQLdb >>> CREATE DATABASE chom; File "<stdin>", line 1 CREATE DATABASE chom; ^ SyntaxError: invalid syntax