python 连接数据库练习
2017-10-18 16:13
435 查看
#!/usr/bin/python
# -*- coding:utf-8 -*-
import logging
logging.basicConfig(level=logging.INFO)
import mysql.connector
import json
def selectmysql():
try:
conn = mysql.connector.connect(host='ip',port=端口号,user='test',passwd='test123456',db='test', use_unicode=True)
cursor = conn.cursor()
data = []
for index in range(1,100):
# 插入一行记录,注意MySQL的占位符是%s:
cursor.execute('select id from user where id = "%s" '% index )
# 查询单数据时用
id = cursor.fetchone()
# id = cursor.fetchall() 查询多数据时使用
#去除为空的数据
if id != None:
data.append(wmid)
print data #打印的数据结果为数组
print json.dumps(data) #转换为json格式
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
conn.commit()
cursor.close
conn.close
print 'Connect test closed in finally'
'''
'cursor.execute('insert into test (id, name) values (%s, %s)', ['1', 'Michael'])
cursor.rowcount
# 提交事务:
conn.commit()
cursor.close()
'''
# -*- coding:utf-8 -*-
import logging
logging.basicConfig(level=logging.INFO)
import mysql.connector
import json
def selectmysql():
try:
conn = mysql.connector.connect(host='ip',port=端口号,user='test',passwd='test123456',db='test', use_unicode=True)
cursor = conn.cursor()
data = []
for index in range(1,100):
# 插入一行记录,注意MySQL的占位符是%s:
cursor.execute('select id from user where id = "%s" '% index )
# 查询单数据时用
id = cursor.fetchone()
# id = cursor.fetchall() 查询多数据时使用
#去除为空的数据
if id != None:
data.append(wmid)
print data #打印的数据结果为数组
print json.dumps(data) #转换为json格式
except mysql.connector.Error as e:
print ('Error : {}'.format(e))
finally:
conn.commit()
cursor.close
conn.close
print 'Connect test closed in finally'
'''
'cursor.execute('insert into test (id, name) values (%s, %s)', ['1', 'Michael'])
cursor.rowcount
# 提交事务:
conn.commit()
cursor.close()
'''
相关文章推荐
- R和python连接SQL sever 数据库操作
- python3数据库配置,远程连接mysql服务器
- 七、Python Django连接数据库
- python使用mysqldb连接数据库
- python3连接Mairadb数据库
- python和数据库连接(转)
- Python连接数据库
- Python数据库的连接
- Python3出现“No module named 'MySQLdb'“问题-以及使用PyMySQL连接数据库
- python -- peewee处理数据库连接
- Python下连接数据库方法
- 学习python之编写简单简单连接数据库并执行查询操作
- Python3.x的mysqlclient的安装、Python操作mysql,python连接MySQL数据库,python创建数据库表,带有事务的操作,CRUD
- Python - 3MySQL 数据库连接
- Python连接SQL Server数据库 - pymssql使用基础
- python 连接操作 各类数据库
- python-连接数据库
- Python3 MySQL 数据库连接
- python连接postgresql数据库
- Python3.6.1版本连接hive数据库