win7上python2.7连接mysql数据库
一:安装mysql
下载地址:http://dev.mysql.com/downloads/installer/
*mysql的安装以及基本sql操作提前预习一下
二:安装MySQL-python驱动
下载地址:http://dev.mysql.com/downloads/connector/python/
根据自己操作系统和python版本选择相对应的驱动
例如(mysql-connector-python-2.1.3-py2.7-winx64.msi)
三:演示如何连接到MySQL服务器的test数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | >>> import mysql.connector >>> conn = mysql.connector.connect( user = 'root' , password = '', database = 'test' ) #连接数据库 >>> cursor = conn.cursor() >>> cursor.execute( 'select * from student where 1=1' ) #表查询 >>> values = cursor.fetchall() >>> values [] >>> cursor.close() True >>> conn.close()>>> import mysql.connector >>> conn = mysql.connector.connect(user = 'root' , password = ' ', database=' test') >>> cursor = conn.cursor() >>> cursor.execute( 'create table user (id varchar(20) primary key, name varchar(20))' ) #建表 >>> cursor.execute( 'insert into user (id, name) values (%s, %s)' , [ '1' , 'Michael' ]) #插入数据 >>> cursor.rowcount 1 >>> conn.commit() >>> cursor.close() True >>> cursor = conn.cursor() >>> cursor.execute( 'select * from user where id = %s' , ( '1' ,)) >>> values = cursor.fetchall() >>> values [(u '1' , u 'Michael' )] >>> cursor.execute( 'update user set name=%s where id=%s' ,( 'zf' , '1' )) #更新 >>> cursor.execute( 'select * from user ' ) >>> values = cursor.fetchall() >>> values [(u '1' , u 'zf' )] |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~