python如何连接mysql

 时间:2024-11-04 16:53:54

1、安装Python开发环境安装MySQL数据库安装Python连接MySQL的依赖包MySQLdb

2、编写代码文件python_connect_mysql.py

3、#! /usr/bin/env python#coding: utf8import MySQLdbif __name__ == "__main__": conn = None try: config = {"host":'127.0.0.1', "port":3306, "user":'test', "passwd":'12345678', "db":'test', "charset":'utf8'} conn = MySQLdb.connect(**config) conn.autocommit(True) cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute("select name,age from tbtest") rows = cursor.fetchall() for row in rows: print row["name"] print row["age"] except Exception, e: print str(e) finally: if conn: conn.close()

4、执行代码:pythonpython_connect_mysql.py

  • 数据库字段约束性特征使用
  • 如何制作windows下redis启动批处理文件bat文件
  • 柔顺剂可以给电器除尘
  • mac版Navicat如何设置总是显示选项卡栏?
  • MySQL基本语法
  • 热门搜索
    香辣排骨的做法 tcl电视怎么投屏 酱排骨的做法 蛋黄酱的做法 临时身份证怎么办理 三鲜饺子馅的做法 猪肚莲子汤的做法 手机怎么投屏到电脑 岗位职责怎么写 暖气片不热怎么办