1.Mysqldb 模块是用于python链接mysql数据库的接口,默认是没有安装的[root@Python ~]# yum install Mysql-python -y2.创建python脚本[root@python ~]#
1.Mysqldb 模块是用于python链接mysql数据库的接口,默认是没有安装的
[root@Python ~]# yum install Mysql-python -y
2.创建python脚本
[root@python ~]# cat mysql.py
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import MySQLdb as mysql #导入MySQLdb模块
db=mysql.connect(user='root',passwd='Centos',db='test',host='localhost') #连接数据库
cursor=db.cursor() #创建游标对象
sql='create table test(id int,name varchar(8));' #创建表
cursor.execute(sql) #执行sql语句
db.close() #关闭连接
3.执行脚本,进库查看是否成功
[root@python ~]# mysql -uroot -pcentos
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
mysql> desc test;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--结束END--
本文标题: Python 连接mysql数据库进行操
本文链接: https://www.lsjlt.com/news/182959.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0