python下使用mysql.connector 安装以及连接数据库的操作
2015-03-29 20:01
1236 查看
1.ubuntn下mysql.connector模块的安装
sudo-apt-get install mysql.connector
2.数据库的连接
config={
'host':'127.0.0.1',#默认127.0.0.1
'user':'root',
'password':'ict',
'port':3306 ,#默认即为3306
'database':'tianyi',
'charset':'utf8'#默认即为utf8
}
try:
cnn=mysql.connector.connect(**config)
if cnn:
print 'ok'
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
3、插入数据
cursor=cnn.cursor()
try:
'第一种:直接字符串插入方式'
sql_insert1="insert into student (name, age) values ('orange', 20)"
cursor.execute(sql_insert1)
'第二种:元组连接插入方式'
sql_insert2="insert into student (name, age) values (%s, %s)"
#此处的%s为占位符,而不是格式化字符串,所以age用%s
data=('shiki',25)
cursor.execute(sql_insert2,data)
'第三种:字典连接插入方式'
sql_insert3="insert into student (name, age) values (%(name)s, %(age)s)"
data={'name':'mumu','age':30}
cursor.execute(sql_insert3,data)
#如果数据库引擎为Innodb,执行完成后需执行cnn.commit()进行事务提交
except mysql.connector.Error as e:
print('insert datas error!{}'.format(e))
finally:
cursor.close()
cnn.close()
同样,MySQL Connector也支持批量插入,同样其使用的也是cursor.executemany,示例如下:
stmt='insert into student (name, age) values (%s,%s)'
data=[
('Lucy',21),
('Tom',22),
('Lily',21)]
cursor.executemany(stmt,data)
4、查询操作
cursor=cnn.cursor()
try:
sql_query='select id,name from student where age > %s'
cursor.execute(sql_query,(21,))
for id,name in cursor:
print ('%s\'s age is older than 25,and her/his id is %d'%(name,id))
except mysql.connector.Error as e:
print('query error!{}'.format(e))
finally:
cursor.close()
cnn.close()
5、删除操作
cursor=cnn.cursor()
try:
sql_delete='delete from student where name = %(name)s and age < %(age)s'
data={'name':'orange','age':24}
cursor.execute(sql_delete,data)
except mysql.connector.Error as e:
print('delete error!{}'.format(e))
finally:
cursor.close()
cnn.close()
sudo-apt-get install mysql.connector
2.数据库的连接
config={
'host':'127.0.0.1',#默认127.0.0.1
'user':'root',
'password':'ict',
'port':3306 ,#默认即为3306
'database':'tianyi',
'charset':'utf8'#默认即为utf8
}
try:
cnn=mysql.connector.connect(**config)
if cnn:
print 'ok'
except mysql.connector.Error as e:
print('connect fails!{}'.format(e))
3、插入数据
cursor=cnn.cursor()
try:
'第一种:直接字符串插入方式'
sql_insert1="insert into student (name, age) values ('orange', 20)"
cursor.execute(sql_insert1)
'第二种:元组连接插入方式'
sql_insert2="insert into student (name, age) values (%s, %s)"
#此处的%s为占位符,而不是格式化字符串,所以age用%s
data=('shiki',25)
cursor.execute(sql_insert2,data)
'第三种:字典连接插入方式'
sql_insert3="insert into student (name, age) values (%(name)s, %(age)s)"
data={'name':'mumu','age':30}
cursor.execute(sql_insert3,data)
#如果数据库引擎为Innodb,执行完成后需执行cnn.commit()进行事务提交
except mysql.connector.Error as e:
print('insert datas error!{}'.format(e))
finally:
cursor.close()
cnn.close()
同样,MySQL Connector也支持批量插入,同样其使用的也是cursor.executemany,示例如下:
stmt='insert into student (name, age) values (%s,%s)'
data=[
('Lucy',21),
('Tom',22),
('Lily',21)]
cursor.executemany(stmt,data)
4、查询操作
cursor=cnn.cursor()
try:
sql_query='select id,name from student where age > %s'
cursor.execute(sql_query,(21,))
for id,name in cursor:
print ('%s\'s age is older than 25,and her/his id is %d'%(name,id))
except mysql.connector.Error as e:
print('query error!{}'.format(e))
finally:
cursor.close()
cnn.close()
5、删除操作
cursor=cnn.cursor()
try:
sql_delete='delete from student where name = %(name)s and age < %(age)s'
data={'name':'orange','age':24}
cursor.execute(sql_delete,data)
except mysql.connector.Error as e:
print('delete error!{}'.format(e))
finally:
cursor.close()
cnn.close()
相关文章推荐
- mysql的安装与简单使用以及myeclipse下与数据库的连接和使用
- Python3.x的mysqlclient的安装、Python操作mysql,python连接MySQL数据库,python创建数据库表,带有事务的操作,CRUD
- python连接sqlite(mysql)数据库以及sqlite数据库基础操作
- VS2012 使用MySql的API函数连接数据库(成功), 以及使用mysql-connector-c++-1.1.3遇到的问题!
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- linux安装mysql和使用c语言操作数据库的方法 c语言连接mysql
- MySQL5.5.22版本安装配置以及基本命令的使用和管理数据库备份与恢复操作详解
- 编写操作数据库的JAVA程序时需要的连接MySQL数据库的JDBC连接包mysql-connector-java-5.1.10.zip怎么安装
- python操作三大主流数据库(3)python操作mysql③python操作mysql的orm工具sqlaichemy安装配置和使用
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- linux安装mysql和使用c语言操作数据库的方法 c语言连接mysql
- 教为学:Python学习之路(二):MySQLdb的几种安装方式,以及用Python测试连接MySql
- 深入探讨:PHP使用数据库永久连接方式操作MySQL的是与非
- Memcached 安装、使用(Python操作)以及常用方法
- Linux 下的MySQL安装 以及数据库的基本操作
- 在PHP中使用MySQL Mysqli操作数据库 ,以及类操作方法
- Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作
- 02-使用C3P0连接池连接MySql并且使用QueryRunner简化数据库操作
- 教为学:Python学习之路(二):MySQLdb的几种安装方式,以及用Python测试连接MySql
- windows下mysql的安装以及与python的连接