全部产品
云市场

Python程序访问

更新时间:2019-03-28 17:40:49

通过MySQLdb

  1. #!/usr/bin/python
  2. import MySQLdb
  3. db = MySQLdb.connect(
  4. host="dsql.aliyuncs.com", # 主机名
  5. port=3306, # 端口号
  6. user="your-dsql-account", # DSQL账号
  7. passwd="your-password") # DSQL密码
  8. cur = db.cursor()
  9. cur.execute("select * from testlink.testdb.testtb where id = 1")
  10. for row in cur.fetchall():
  11. print(row[0])
  12. db.close()

通过PyMySQL

  1. import pymysql
  2. conn = pymysql.connect(host='dsql.aliyuncs.com',port='3306', user='your-dsql-account', passwd='your-password')
  3. cur = conn.cursor()
  4. cur.execute("SELECT * from testlink.testdb.testtb where id = 1")
  5. for r in cur:
  6. print(r)
  7. cur.close()
  8. conn.close()