python操作mysql数据库
2014-04-13 11:35
344 查看
python操作mysql数据库,查询出来数据库的值,并保存到文件中:
#-*-coding:utf-8-*-
import sys
import MySQLdb #引入mysql模块
reload(sys)
sys.setdefaultencoding('utf-8') #这两段话是改变python的编码
conn = MySQLdb.connect(user='root',passwd='root',host='127.0.0.1',charset='utf8') #连接数据库
conn.select_db('huang') #选择数据库
cur = conn.cursor() #查询出来的数据保存在一个cursor中
cur.execute("insert into user(username,passwd) values('kk','kk')") #插入一条数据
s = cur.execute('select * from user') #查询
file = open('1.txt','r+') #打开一个文件
desc = cur.description #cursor的头部
username = str(desc[0][0])
passwd = str(desc[1][0])
id = str(desc[2][0])
length = len(username)
file.write(username+' '*(15-length))
length = len(passwd)
file.write(passwd+' '*(15-length))
length = len(id)
file.write(id+' '*(15-length)+'\n') #表头部分
for n in cur.fetchmany(s): #取出来查询出来的值
for x in n:
x = str(x)
print x
length = len(x)
file.write(str(x)+' '*(15-length))
file.write('\n')
file.close() #关闭
conn.commit()
cur.close()
conn.close()数据库中的表:
username passwd id
admin admin 1
huangteng huangteng 2
kk kk 44
荒唐 kk 45
kk kk 46
第一次做练习,感觉python写程序真的好厉害啊,比Java简单,但是实现的功能也不比Java少
#-*-coding:utf-8-*-
import sys
import MySQLdb #引入mysql模块
reload(sys)
sys.setdefaultencoding('utf-8') #这两段话是改变python的编码
conn = MySQLdb.connect(user='root',passwd='root',host='127.0.0.1',charset='utf8') #连接数据库
conn.select_db('huang') #选择数据库
cur = conn.cursor() #查询出来的数据保存在一个cursor中
cur.execute("insert into user(username,passwd) values('kk','kk')") #插入一条数据
s = cur.execute('select * from user') #查询
file = open('1.txt','r+') #打开一个文件
desc = cur.description #cursor的头部
username = str(desc[0][0])
passwd = str(desc[1][0])
id = str(desc[2][0])
length = len(username)
file.write(username+' '*(15-length))
length = len(passwd)
file.write(passwd+' '*(15-length))
length = len(id)
file.write(id+' '*(15-length)+'\n') #表头部分
for n in cur.fetchmany(s): #取出来查询出来的值
for x in n:
x = str(x)
print x
length = len(x)
file.write(str(x)+' '*(15-length))
file.write('\n')
file.close() #关闭
conn.commit()
cur.close()
conn.close()数据库中的表:
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50067 Source Host : localhost:3306 Source Database : huang Target Server Type : MYSQL Target Server Version : 50067 File Encoding : 65001 Date: 2014-04-13 11:34:22 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `username` varchar(50) default NULL, `passwd` varchar(50) default NULL, `id` int(15) NOT NULL auto_increment, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('admin', 'admin', '1'); INSERT INTO `user` VALUES ('huangteng', 'huangteng', '2'); INSERT INTO `user` VALUES ('kk', 'kk', '44'); INSERT INTO `user` VALUES ('荒唐', 'kk', '45'); INSERT INTO `user` VALUES ('kk', 'kk', '46');查询出来写到文件中的结果:
username passwd id
admin admin 1
huangteng huangteng 2
kk kk 44
荒唐 kk 45
kk kk 46
第一次做练习,感觉python写程序真的好厉害啊,比Java简单,但是实现的功能也不比Java少
相关文章推荐
- Python操作MySQL数据库
- python 3.6 +pyMysql 操作mysql数据库
- python 操作MySQL数据库使用"insert into ... on duplicate key update"报错问题
- python操作mysql数据库
- python操作MySQL数据库
- python 操作MySQL数据库
- python操作mysql数据库的相关操作实例
- python 通过mysqldb 操作mysql数据库
- python操作MySQL数据库
- 使用python操作mysql数据库
- python操作mysql数据库
- Python入门学习教程:数据库操作,连接MySql数据库
- Python实现连接操作MySql数据库
- python操作MySQL数据库
- python连接mysql数据库示例(做增删改操作)
- Python操作MySQL数据库
- python 操作mysql数据库(简单/基础)
- python3操作mysql数据库增删改查
- Python操作mysql数据库
- python操作MySQL数据库