Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating database in python

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
like image 255
chom Avatar asked Apr 18 '26 15:04

chom


1 Answers

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.

like image 177
Burhan Khalid Avatar answered Apr 21 '26 04:04

Burhan Khalid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!