用python链接mysql

对应python3以上的版本:

需要在pip上安装pymysql的包,我在引入这个包时遇到了无法引入的情况,No module named 'PyMySQL'

查了一遍之后,应该是pip解释器和编码用的不一致导致。

解决方案:

 将此路径下文件拷贝到编译器环境即可(缺省拷贝,不用覆盖,可能会多拷一些东西,删除就好)

连接代码如下:

  import pymysql

server = "localhost"
user = "root"
password = "000000"
database = "lightning"

db=pymysql.connect(server, user, password, database)
#使用cursor()方法创建一个游标对象cursor
cursor =db.cursor()
#使用execute()方法执行SQL查询
#str="INSERT INTO `lightning`.`lightning_count` (`pic_Name`, `sex`, `start_time`, `end_time`, `intensity`, `start_longitude`, `start_latitude`, `end_longitude`, `end_latitude`, `x0`, `y0`, `x1`, `y1`) VALUES ('D:/002.png', 'I', '2020-06-22 19:56:10.156', '2020-06-22 19:56:10.856', '13999', '35.987000', '42.578000', '36.786000', '44.562000', '100', '100', '200', '200');"
#cursor.execute("use  lightning;")
cursor.execute("INSERT INTO lightning.lightning_count (Id) VALUES (3)")
cursor.connection.commit()#执行commit操作,插入语句才能生效
#使用fetchone()方法获取单条数据,并打印
#data =cursor.fetchone()
#print("Datebase version : %s " % data)
#关闭数据库连接
db.close()

猜你喜欢

转载自www.cnblogs.com/fengxiaokang/p/13181469.html
今日推荐