简体   繁体   中英

I want to make a table in mysql using python and i get this error but i have data base named sam what should i do

import pymysql as ps

con = ps.connect('localhost','root','','sam') #connecting to the database

print("connected ! ! !")
c = con.cursor() #were are we in database

com="""create table u(
        code int PRIMARY KEY,
        name varchar(20),
        family varchar(20),
        age int(3)) """


c.execute(com)
con.commit()

con.close()

[Exception has occurred: OperationalError (1049, "Unknown database 'sam'") File "E:\script\python mySQL\making_new_sql.py", line 3, in con = pymysql.connect('localhost','root','','sam') #connecting to the database]

You need to create the database with the same name as sam in order to access it through python. Use the MYSQL console that came with your installation to create the database. use the command:

CREATE DATABASE sam

Then you can go ahead and add the table through python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM