首先在pycharm中安装mtsqlclient的包
等待安装完成以后,进入编辑页面就可以正常编程了。举例如下:
import MySQLdb
#import situation as sit
conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='mysql',
db ='produce',
)
#通过获取到的数据库连接conn下的cursor()方法来创建游标。
cur = conn.cursor()
#创建数据表,通过游标cur 操作execute()方法可以写入纯sql语句。通过execute()方法中写如sql语句来对数据进行操作
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
hhh=5
#插入一条数据
cur.execute("insert into temp set a = %s" % (hhh) ) #values('2','Tom','3 year 2 class','9')")
#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")
#删除查询条件的数据
#cur.execute("delete from student where age='9'")
#cur.close() 关闭游标
cur.close()
#conn.commit()方法在提交事物,在向数据库插入一条数据时必须要有这个方法,否则数据不会被真正的插入。
conn.commit()
#conn.close()关闭数据库连接
conn.close()