Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. conn = mysql.connector.connect(user='root', password='1029384756', database='t1')
  4. cursor = conn.cursor()
  5. # 创建user表:
  6. # cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
  7. # 插入一行记录,注意MySQL的占位符是%s:
  8. # cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael'])
  9. cursor.rowcount
  10. # 1
  11. # 提交事务:
  12. conn.commit()
  13. cursor.close()
  14. # 运行查询:
  15. cursor = conn.cursor()
  16. cursor.execute('select * from user where id = %s', ('1',))
  17. values = cursor.fetchall()
  18. print(values)
  19. # [('1', 'Michael')]
  20. # 关闭Cursor和Connection:
  21. cursor.close()
  22. True
  23. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement