美文网首页
pymysql使用杂记(一)

pymysql使用杂记(一)

作者: MintSakura | 来源:发表于2017-07-21 17:42 被阅读0次

<strong>下载方式:</strong>
<strong>直接pip install</strong>
<pre><code>pip install PyMySQL</code></pre>

<strong>或者也可以手动下载:</strong>
https://pypi.python.org/pypi/PyMySQL/
下载与环境匹配的whl文件,并把文件放在\Script文件夹里面再
<pre><code>pip install xxxx.whl</code></pre>

<strong>使用:</strong>
<pre><code>#!/usr/bin/python3

import pymysql

打开数据库连接

db = pymysql.connect("localhost","testuser","test123","TESTDB" )

使用 cursor() 方法创建一个游标对象 cursor

cursor = db.cursor()

使用 execute() 方法执行 SQL,如果表存在则删除

cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

使用预处理语句创建表

sql = """CREATE TABLE EMPLOYEE (

FIRST_NAME CHAR(20) NOT NULL,

LAST_NAME CHAR(20),

AGE INT,

SEX CHAR(1),

INCOME FLOAT )"""

cursor.execute(sql)

关闭数据库连接

db.close()</pre></code>

<em><strong>
ps:不过个人认为与其用pymysql创建数据库,不如直接用mysql导入.sql文件
</em></strong>

相关文章

网友评论

      本文标题:pymysql使用杂记(一)

      本文链接:https://www.haomeiwen.com/subject/qkkqkxtx.html