python操作mysql数据库实例
2014-09-18 21:41
691 查看
Python代码
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
#建立和数据库系统的连接
conn = MySQLdb.connect(host='localhost',
user='root',passwd='longforfreedom')
#获取操作游标
cursor = conn.cursor()
#执行SQL,创建一个数据库.
cursor.execute("""create database python
""")
#关闭连接,释放资源
cursor.close();
创建数据库,创建表,插入数据,插入多条数据
Python代码
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
#建立和数据库系统的连接
conn = MySQLdb.connect(host='localhost',
user='root',passwd='longforfreedom')
#获取操作游标
cursor = conn.cursor()
#执行SQL,创建一个数据库.
cursor.execute("""create database if
not exists python""")
#选择数据库
conn.select_db('python');
#执行SQL,创建一个数据表.
cursor.execute("""create table test(id
int, info varchar(100)) """)
value = [1,"inserted
?"];
#插入一条记录
cursor.execute("insert into test values(%s,%s)",value);
values=[]
#生成插入参数值
for i in range(20):
values.append((i,'Hello
mysqldb, I am recoder ' + str(i)))
#插入多条记录
cursor.executemany("""insert into test values(%s,%s) """,values);
#关闭连接,释放资源
cursor.close();
Python代码
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
conn = MySQLdb.connect(host='localhost', user='root',
passwd='longforfreedom',db='python')
cursor = conn.cursor()
count = cursor.execute('select * from test')
print '总共有
%s 条记录',count
#获取一条记录,每条记录做为一个元组返回
print "只获取一条记录:"
result = cursor.fetchone();
print result
#print 'ID: %s info: %s' % (result[0],result[1])
print 'ID:
%s info: %s' % result
#获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录
获取多条记录
print "只获取5条记录:"
results = cursor.fetchmany(5)
for r in results:
print r
print "获取所有结果:"
#重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,
cursor.scroll(0,mode='absolute')
#获取所有结果
results = cursor.fetchall()
for r in results:
print r
conn.close()
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
#建立和数据库系统的连接
conn = MySQLdb.connect(host='localhost',
user='root',passwd='longforfreedom')
#获取操作游标
cursor = conn.cursor()
#执行SQL,创建一个数据库.
cursor.execute("""create database python
""")
#关闭连接,释放资源
cursor.close();
创建数据库,创建表,插入数据,插入多条数据
Python代码
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
#建立和数据库系统的连接
conn = MySQLdb.connect(host='localhost',
user='root',passwd='longforfreedom')
#获取操作游标
cursor = conn.cursor()
#执行SQL,创建一个数据库.
cursor.execute("""create database if
not exists python""")
#选择数据库
conn.select_db('python');
#执行SQL,创建一个数据表.
cursor.execute("""create table test(id
int, info varchar(100)) """)
value = [1,"inserted
?"];
#插入一条记录
cursor.execute("insert into test values(%s,%s)",value);
values=[]
#生成插入参数值
for i in range(20):
values.append((i,'Hello
mysqldb, I am recoder ' + str(i)))
#插入多条记录
cursor.executemany("""insert into test values(%s,%s) """,values);
#关闭连接,释放资源
cursor.close();
Python代码
#!/usr/bin/env python
#coding=utf-8
import MySQLdb
conn = MySQLdb.connect(host='localhost', user='root',
passwd='longforfreedom',db='python')
cursor = conn.cursor()
count = cursor.execute('select * from test')
print '总共有
%s 条记录',count
#获取一条记录,每条记录做为一个元组返回
print "只获取一条记录:"
result = cursor.fetchone();
print result
#print 'ID: %s info: %s' % (result[0],result[1])
print 'ID:
%s info: %s' % result
#获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录
获取多条记录
print "只获取5条记录:"
results = cursor.fetchmany(5)
for r in results:
print r
print "获取所有结果:"
#重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,
cursor.scroll(0,mode='absolute')
#获取所有结果
results = cursor.fetchall()
for r in results:
print r
conn.close()
相关文章推荐
- Python操作MySQL数据库实例
- Python操作MySQL数据库9个实用实例
- Python操作MySQL数据库9个实用实例
- python 3.6 +pyMysql 操作mysql数据库(实例讲解)
- Python3.6简单的操作Mysql数据库的三个实例
- python爬虫实践学习实例之mysql数据库操作
- Python操作MySQL数据库实例
- Mysql入门笔记(2):Python操作MySQL数据库9个实用实例
- python操作mysql数据库实例
- Python操作MySQL数据库9个实用实例
- Python操作MySQL数据库9个实用实例
- Python操作MySQL数据库9个实用实例
- Python操作MySQL数据库9个实用实例
- python通过elixir包操作mysql数据库实例代码
- Python MySQLdb模块连接操作mysql数据库实例_python
- Python操作使用MySQL数据库的实例代码
- Python操作MySQL数据库9个实用实例
- Python MySQLdb模块连接操作mysql数据库实例
- python操作mysql数据库增删改查的dbutils实例
- python操作mysql数据库的相关操作实例