3

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
Ashwini Chaudhary
252k60 gold badges479 silver badges520 bronze badges
asked Jun 13, 2013 at 4:09

2 Answers 2

9

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.

answered Jun 13, 2013 at 4:11
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the solution. I am getting the following error. Do i need to add Mysql path to environmental variables ? If so what path i need to add ? C:\>mysql -u Chom -p 'mysql' is not recognized as an internal or external command, operable program or batch file.
Did you install a MySQL server?
Technically creating the database is possible from python, "should be run from the MySQL command line client" is a subjective statement.
Can we use the same thing for an SQLite database using python ?
username is not input of connect function. please replace it with user
3

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)
answered Jun 13, 2013 at 4:15

2 Comments

Thanks for the solution. I am getting the following error. <br/> >>> db1 = MySQLdb.connect(host="localhost",user="Chom",passwd="") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\MySQLdb_init_.py", line 81, in Connect return Connection(*args, **kwargs) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in init super(Connection, self).__init__(*args, **kwargs2)_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
@chom Maybe you should check you mysql status.The username、password and port.Make sure your mysql service works well and can be connected with others.I think the error "Can't connect to MySQL server on 'localhost' (10061)" is caused by your mysql settings not python code.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.