python 使用MySQLdb连接mysql,pythonmysqldb,MySQLdb顾名思义,
python 使用MySQLdb连接mysql,pythonmysqldb,MySQLdb顾名思义,
MySQLdb顾名思义,这个包是python连接mysql的包
# 打开数据库conn = MySQLdb.connect(**self.options.mysql_conn_params)cursor = conn.cursor()table_name = "oostat_%d_d" % (today.year, today.month)# 创建数据表try: if new_table:sql = "CREATE TABLE IF NOT EXISTS %s LIKE oostat" % table_namecursor.execute(sql)except Exception as e: log.err(e)# 数据入库try: sql = "INSERT INTO " + table_name + "(devid, name, time, stat) VALUES(%s, %s, NOW(), %s)" cursor.executemany(sql, values)except Exception as e: log.err(e)# 关闭连接cursor.close()conn.commit() # ! 重要,不要忘了提交conn.close()
注意事项:
连接时一定要指定 charset,否则是 latin-1,不管 mysql 是什么样的配置修改数据后,关闭连接前,一定要执行 commit,执行了大不了是个空操作,不执行可能数据就不知道哪里去了可以用 executemany 执行多个查询
相关内容
- python处理JSON中的浮点数不精确问题,pythonjson浮点数,有
- python 缩略图,给图片加水印,,这里使用了PIL类库。[
- python比较两个文件的差异,python两个差异,下面的pytho
- Python 求解组合问题,python求解组合,[Python]代码de
- Python sqlite模块,pythonsqlite模块,sqlite是非常著名的
- Python删除指定目录下的过期文件,,实现类似该命令的操
- MySQLdb and threading Base Usage,mysqldbthreading,[Python]代码im
- Python 读取纯真IP数据库,pythonip数据库,[Python]代码#!
- Python RSS阅读器,pythonrss阅读器,[Python]代码#
- python字符串填充,python字符串,string1 = "N
评论关闭