Python Module: pymysql

Frist we should have a PyMySQL package, let me can import the module.


Then we can use PyMySQL in Python for work.


#!/usr/bin/python
import pymysql
#create mysql connect
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='derbysoft', db='python')
#get cursor
cur = conn.cursor()
#delete Table
delete_table = "DROP TABLE `test`;"
cur.execute(delete_table)
print(delete_table)


we can use help(cur) search something useful function.

wKioL1MdOtLzCXn8AAFwFdEaW1g870.jpg


Pay Attention

If we want to INSERT something to the DataBase, we need use conn.commit() or else the INSERT sentence will not working.

http://stackoverflow.com/questions/20485332/pymysql-insert-into-not-working

for x in range(10):
    insert_data = "INSERT INTO `python`.`test` (`id`, `name`) VALUES ('%d', '%s');" % (x, 'Nick')
    cur.execute(insert_data)
    conn.commit()
    print(insert_data)




你可能感兴趣的:(python,pymysql)