Guest User

Untitled

a guest
Jan 8th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. # 导入pymysql模块
  2. import pymysql
  3.  
  4. # 连接database
  5. conn = pymysql.connect(
  6. host=“你的数据库地址”,
  7. user=“用户名”,password=“密码”,
  8. database=“数据库名”,
  9. charset=“utf8”)
  10.  
  11. # 得到一个可以执行SQL语句的光标对象
  12. cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
  13. # 得到一个可以执行SQL语句并且将结果作为字典返回的游标
  14. #cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
  15.  
  16. # 定义要执行的SQL语句
  17. sql = """
  18. CREATE TABLE USER1 (
  19. id INT auto_increment PRIMARY KEY ,
  20. name CHAR(10) NOT NULL UNIQUE,
  21. age TINYINT NOT NULL
  22. )ENGINE=innodb DEFAULT CHARSET=utf8; #注意:charset='utf8' 不能写成utf-8
  23. """
  24.  
  25. # 执行SQL语句
  26. cursor.execute(sql)
  27.  
  28. # 关闭光标对象
  29. cursor.close()
  30.  
  31. # 关闭数据库连接
  32. conn.close()
Add Comment
Please, Sign In to add comment