python学习之数据库操作(mysql_ubuntu版)
来源:互联网 发布:数据库云备份 编辑:程序博客网 时间:2024/06/05 14:49
mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法。python操作数据库需要安装一个第三方的模块,在http://mysql-python.sourceforge.net/有下载和文档。
下面给出ubuntu安装说明:
在终端中输入:sudo apt-get install python-mysqldb
OK,搞定,简单吧?来测试下
安装完成之后可以在Python解释器中测试一下
IDE输入 import MySQLdb #注意大小写
或终端输入python MySQLdb
如果不报错,就证明安装成功了。
#-*- encoding: gb2312 -*-
import os, sys, string
import MySQLdb
# 连接数据库
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')
except Exception, e:
print e
sys.exit()
# 获取cursor对象来进行操作
cursor = conn.cursor()
# 创建表
sql = "create table if not exists test1(name varchar(128) primary key, age int(4))"
import os, sys, string
import MySQLdb
# 连接数据库
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')
except Exception, e:
print e
sys.exit()
# 获取cursor对象来进行操作
cursor = conn.cursor()
# 创建表
sql = "create table if not exists test1(name varchar(128) primary key, age int(4))"
cursor.execute(sql)
# 插入数据
sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)
try:
# 插入数据
sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)
try:
cursor.execute("set NAMES utf8") #乱码问题
cursor.execute(sql)
except Exception, e:
print e
sql = "insert into test1(name, age) values ('%s', %d)" % ("张三", 21)
try:
cursor.execute(sql)
except Exception, e:
print e
sql = "insert into test1(name, age) values ('%s', %d)" % ("张三", 21)
try:
cursor.execute("set NAMES utf8")
cursor.execute(sql)
except Exception, e:
print e
# 插入多条
sql = "insert into test1(name, age) values (%s, %s)"
val = (("李四", 24), ("王五", 25), ("洪六", 26))
try:
except Exception, e:
print e
# 插入多条
sql = "insert into test1(name, age) values (%s, %s)"
val = (("李四", 24), ("王五", 25), ("洪六", 26))
try:
cursor.execute("set NAMES utf8")
cursor.executemany(sql, val)
except Exception, e:
print e
#查询出数据
sql = "select * from test1"
cursor.executemany(sql, val)
except Exception, e:
print e
#查询出数据
sql = "select * from test1"
cursor.execute("set NAMES utf8")
cursor.execute(sql)
alldata = cursor.fetchall()
# 如果有数据返回,就循环输出, alldata是有个二维的列表
if alldata:
for rec in alldata:
print rec[0], rec[1]
cursor.close()
conn.close()
cursor.execute(sql)
alldata = cursor.fetchall()
# 如果有数据返回,就循环输出, alldata是有个二维的列表
if alldata:
for rec in alldata:
print rec[0], rec[1]
cursor.close()
conn.close()
- python学习之数据库操作(mysql_ubuntu版)
- Python学习之数据库操作
- python之数据库操作
- python之数据库操作
- Python之操作MySQL数据库(二)
- Python学习总结笔记(9)-- MySQL数据库操作之mysql-connector
- Python学习总结笔记(10)-- MySQL数据库操作之SQLAlchemy使用总结
- MySQL数据库操作学习(Python)
- Python学习笔记-数据库操作
- python 学习 (三) python利用mysqldb操作数据库
- Python学习笔记(三)Python数据库操作。
- python之mysql数据库操作
- Python之操作MySQL数据库
- python操作数据库之pymysql
- 用python操作mysql数据库(之简单查操作)
- Python之学习笔记(IO操作)
- python 学习之路(文件操作)
- python基础学习-python操作sqlite数据库
- 通过文件路径获得文件大小
- 利用tree匹配user_agent
- Flex Socket安全策略<policy-file-request/>及应对方法
- Sql 远程连接
- JSP四个作用域
- python学习之数据库操作(mysql_ubuntu版)
- WebKit General Orientation
- Ext.form.TextField基本用法
- zz正则表达式语法
- android 退出activity 转吖转
- 关于vs2010+ArcEngine 的”测到LoaderLock“异常
- 135
- linux 块延迟清除 ora-01555
- Oracle SQL Trace 和 10046 事件 .