#!/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()
'''