python 操作mysql数据库

import MySQLdb
   
  try:
  conn_remote = MySQLdb.connect(host='10.220.156.34',user='dc_user',passwd='dc_user',db='SZWG_ECOMON',port=3306)
  #conn_local = MySQLdb.connect(host='10.46.190.21', user='mysql', passwd='mhxzkhl',db='test', port=3306)
  conn_local = MySQLdb.connect(host='localhost', user='mysql', passwd='mhxzkhl',db='test', port=3306)
   
  cur_remote = conn_remote.cursor()
  cur_local = conn_local.cursor()
   
  # cur_remote.execute('select * from job_info where user=\'dt-udw-etl\' limit 10')
  # cur_remote.execute('select * from job_info where user=\'dt-udw-insight\' limit 10')
  cur_remote.execute('select * from job_info limit 3422873')
  results = cur_remote.fetchall();
  for row in results:
  for i in row:
  print str(i)+" ",
  print
  values = []
  for row in results:
  values = []
  for i in range(18):
  values.append(str(row[i]))
  job_name = str(row[1])
  values.append(job_name[0:job_name.find('_2013')])
  #print"++++++++++++++++++++"+str(job_name[0:job_name.find('_2013')])
  #print "values-------------------------------"+str(values)
  cur_local.execute('insert into job_info_new values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',values)
  #print "invinsible seperator line-----------------------------------"
   
  cur_local.execute('select * from job_info limit 10')
  results_2 = cur_local.fetchall()
  for row in results_2:
  print row
  conn_local.commit()
  cur_remote.close()
  conn_remote.close()
  cur_local.close()
  conn_local.close()
  except MySQLdb.Error,e:
  print "Mysql Error %d: %s" % (e.args[0], e.args[1])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值