sudo apt-get install MySQLdb
python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
CREATE DATABASE study; use study; DROP TABLE IF EXISTS python_demo; CREATE TABLE python_demo ( id int NOT NULL AUTO_INCREMENT COMMENT '主键,自增', user_no int NOT NULL COMMENT '用户编号', user_name VARBINARY(50) NOT NULL COMMENT '用户名', password VARBINARY(50) NOT NULL COMMENT '用户密码', remark VARBINARY(255) NOT NULL COMMENT '用户备注', PRIMARY KEY (id,user_no) )ENGINE =innodb DEFAULT CHARSET = utf8 COMMENT '用户测试表'; INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1001,'张三01','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1002,'张三02','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1003,'张三03','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1004,'张三04','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1005,'张三05','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1006,'张三06','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1007,'张三07','admin','我是张三'); INSERT INTO python_demo(user_no, user_name, password, remark) VALUES (1008,'张三08','admin','我是张三');
# --coding=utf8--
import ConfigParser
import sys
import MySQLdb
def init_db():
try:
conn = MySQLdb.connect(host=conf.get('Database', 'host'),
user=conf.get('Database', 'user'),
passwd=conf.get('Database', 'passwd'),
db=conf.get('Database', 'db'),
charset='utf8')
return conn
except:
print "Error:数据库连接错误"
return None
def select_demo(conn, sql):
try:
cursor = conn.cursor()
cursor.execute(sql)
return cursor.fetchall()
except:
print "Error:数据库连接错误"
return None
def update_demo():
pass
def delete_demo():
pass
def insert_demo():
pass
if __name__ == '__main__':
conf = ConfigParser.ConfigParser()
conf.read('mysql.conf')
conn = init_db()
sql = "select * from %s" % conf.get('Database', 'table')
data = select_demo(conn, sql)
pass
select replace(replace(replace(column1,'\r',''),'\n',''),'\t','')
CREATE TABLE `filter_fields` ( `field1` varchar(50) DEFAULT NULL, `field2` varchar(50) DEFAULT NULL, `field3` varchar(50) DEFAULT NULL, `field4` varchar(50) DEFAULT NULL, `field5` varchar(50) DEFAULT NULL, `field6` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test01','test02','test03','test04','test05','test06');
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test11\ntest11','test12\n\n','test13','test14','test15','test16');
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test21\ttest21','test22\ttest22\ttest22','test23\t\t\t','test4','test5','test6');
insert into filter_fields(field1,field2,field3,field4,field5,field6) VALUES
('test21\rest21','test22\r\rest22\r\rest22','test23\r\r\r','test4','test5','test6');
# coding=utf-8
import MySQLdb
import sys
db_host = '127.0.0.1' # 数据库地址
db_port = 3306 # 数据库端口
db_user = 'root' # mysql用户名
db_pwd = 'yourpassword' # mysql用户密码,换成你的密码
db_name = 'test' # 数据库名
db_table = 'filter_fields' # 数据库表
# 过滤sql字段结果中的\t\n
def extract_data(table_name):
try:
conn = MySQLdb.connect(host=db_host, port = db_port, user=db_user,
passwd = db_pwd, db = db_name, charset = "utf8")
cursor = conn.cursor()
except MySQLdb.Error, e:
print '数据库连接异常'
sys.exit(1)
try:
sql = 'select * from %s;'%(table_name)
cursor.execute(sql)
rows = cursor.fetchall()
print '====字段未过滤查询结果===='
for row in rows:
print row
print '====字段过滤之后结果===='
rows_list = []
for row in rows:
row_list = []
for column in row:
row_list.append(column.replace('\t', '').replace('\n', '').replace('\r', ''))
rows_list.append(row_list)
print rows_list[-1] # [-1]表示列表最后一个元素
return rows_list
except MySQLdb.Error, e:
print '执行sql语句失败'
cursor.close()
conn.close()
sys.exit(1)
if __name__ == '__main__':
print 'begin:'
rows = extract_data(db_table)
pass
(u'test01', u'test02', u'test03', u'test04', u'test05', u'test06') (u'test11\ntest11', u'test12\n\n', u'test13', u'test14', u'test15', u'test16') (u'test21\ttest21', u'test22\ttest22\ttest22', u'test23\t\t\t', u'test4', u'test5', u'test6') (u'test21\rest21', u'test22\r\rest22\r\rest22', u'test23\r\r\r', u'test4', u'test5', u'test6')
[u'test01', u'test02', u'test03', u'test04', u'test05', u'test06'] [u'test11test11', u'test12', u'test13', u'test14', u'test15', u'test16'] [u'test21test21', u'test22test22test22', u'test23', u'test4', u'test5', u'test6'] [u'test21est21', u'test22est22est22', u'test23', u'test4', u'test5', u'test6']
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有