Python2.7 with MySQL Connector/Python Sample(Windows OS)

Install the MySQL Connector

Install package

Double click the package and click the Run button to complete installation.

Test if installed successfully

Open windows command line

py
>>> import mysql.connector

If there is no error then it is successful.

Code with example

import mysql.connector
from mysql.connector import errorcode
database = 'testpython'

def get_db_connectoin(database):
    username = 'root'
    password = '******'
    hostname = 'localhost'
    port = 3306
    database = database
    connection = None 
    try:
        connection = mysql.connector.connect(user=username, password=password, host=hostname, port=port, database=database)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print 'Username,Password Error'
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print 'Database does not exist'
            connection = mysql.connector.connect(user=username, password=password, host=hostname, port=port)
            cursor = connection.cursor()
            cursor.execute("CREATE DATABASE "+ database + " DEFAULT CHARACTER SET 'utf8'")
        else:
            print err
    else:
        connection.database = database
    return connection

def create_table(connection, sql):
    cursor = connection.cursor()
    try:
        cursor.execute(sql)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
            print "Table already exist"
        else:
            print err
    else:
        cursor.close()
        connection.close()
    return

def inset_data(connection, sql, args):
    cursor = connection.cursor()
    try:
        cursor.execute(sql, args)
        connection.commit()
    except mysql.connector.Error as err:
        print err
    else:
        cursor.close()
        connection.close()
    return

def query_data(connection, sql, args):
    cursor = connection.cursor()
    cursor.execute(sql, args)
    return cursor

connection = get_db_connectoin(database)
createSql = ("CREATE TABLE `TEST_TABLE` ("
             "`ID` INT(11) PRIMARY KEY AUTO_INCREMENT,"
             "`NAME` VARCHAR(64) NOT NULL,"
             "`AGE` INT(3))"
            )
create_table(connection=connection, sql=createSql)

connection = get_db_connectoin(database)
insetSql = "INSERT INTO TEST_TABLE(NAME,AGE) VALUES (%(NAME)s,%(AGE)s)" 
args = {'NAME':'jason','AGE':28}
inset_data(connection=connection, sql=insetSql, args=args)

connection = get_db_connectoin(database)
querySql = "SELECT * FROM TEST_TABLE WHERE NAME=%s AND AGE > %s"
args = ("jason", 26)
cursor = query_data(connection=connection, sql=querySql, args=args)
for (id, name, age) in cursor:
    print id
    print name
    print age
    print '\n'
cursor.close()
connection.close()

References:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值