python连数据库课程设计_python 连接数据库练习

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

'''

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值