<strong>下載方式:</strong>
<strong>直接pip install</strong>
<pre><code>pip install PyMySQL</code></pre>
<strong>或者也可以手動下載:</strong>
https://pypi.python.org/pypi/PyMySQL/
下載與環(huán)境匹配的whl文件,并把文件放在\Script文件夾里面再
<pre><code>pip install xxxx.whl</code></pre>
<strong>使用:</strong>
<pre><code>#!/usr/bin/python3
import pymysql
打開數(shù)據(jù)庫連接
db = pymysql.connect("localhost","testuser","test123","TESTDB" )
使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
使用 execute() 方法執(zhí)行 SQL,如果表存在則刪除
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
使用預(yù)處理語句創(chuàng)建表
sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
cursor.execute(sql)
關(guān)閉數(shù)據(jù)庫連接
db.close()</pre></code>
<em><strong>
ps:不過個人認(rèn)為與其用pymysql創(chuàng)建數(shù)據(jù)庫,不如直接用mysql導(dǎo)入.sql文件
</em></strong>