Excerpt |
---|
MySQL Connector/Python enables Python programs to access MySQL databases, using an API that is compliant with the Python Database API Specification v2.0 (PEP 249). It also contains an implementation of the X DevAPI, an Application Programming Interface for working with the MySQL Document Store. |
Code Block |
---|
sudo yum -y install mysql-connector-python |
...
Code Block |
---|
$ sudo yum install -y mysql-connector-python
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 25 kB 00:00
* base: asi-fs-w.contabo.net
Excluding mirror: mirror.sfo12.us.leaseweb.net
* epel: ziply.mm.fcix.net
* extras: mirror.fcix.net
* remi-php74: ftp.riken.jp
* remi-safe: ftp.riken.jp
Excluding mirror: mirror.mia11.us.leaseweb.net
* updates: asi-fs-w.contabo.net
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
mysql-connectors-community | 2.6 kB 00:00:00
mysql-tools-community | 2.6 kB 00:00:00
mysql57-community | 2.6 kB 00:00:00
remi-php74 | 3.0 kB 00:00:00
remi-safe | 3.0 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(2/3): epel/x86_64/primary_db | 7.0 MB 00:00:00
(3/3): remi-safe/primary_db | 2.3 MB 00:00:02
Resolving Dependencies
--> Running transaction check
---> Package mysql-connector-python.x86_64 0:8.0.23-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================
Package Arch Version Repository Size
===============================================================================================================
Installing:
mysql-connector-python x86_64 8.0.23-1.el7 mysql-connectors-community 11 M
Transaction Summary
===============================================================================================================
Install 1 Package
Total download size: 11 M
Installed size: 51 M
Downloading packages:
mysql-connector-python-8.0.23-1.el7.x86_64.rpm | 11 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-connector-python-8.0.23-1.el7.x86_64 1/1
Verifying : mysql-connector-python-8.0.23-1.el7.x86_64 1/1
Installed:
mysql-connector-python.x86_64 0:8.0.23-1.el7
Complete! |
Reference class for ckmysql.py
Code Block |
---|
language | py |
---|
title | ckmysql.py |
---|
|
"""
" Copyright ckii.com <http://ckii.com>
" ===================================================
"
" @file _lib/ckmysql.py
" @brief ckmysql
" @author Chun Kang (ck@ckii.com)
"
" @notes
" 2023.01.17 created
"
"""
import mysql.connector
class ckmysql(object):
# Static variable for object member
user = ''
password = ''
host = ''
database = ''
connector = None
cursor = None
def __init__( self, user='nobody', password='my_password', host='127.0.0.1', database='my_db'):
self.user = user
self.password = password
self.host = host
self.database = database
def init(self):
if self.connector == None:
self.connector = mysql.connector.connect( user=self.user, password=self.password, host=self.host, database=self.database)
self.cursor = self.connector.cursor()
return
def query( self, sql):
self.init()
rows = None
if (self.cursor):
self.cursor.execute(sql)
rows = self.cursor.fetchall()
return rows
|
Below example shows the example to use the class defined in ckmysql.py
Code Block |
---|
|
from ckmysql import ckmysql
db = ckmysql() # if you want to use the default settings defined in the class
r = db.query("select * from test limit 5")
print(r) |
Above case is based in using the default database connection information and below is based in the connection environment like user_id, password, server and database.
Code Block |
---|
from ckmysql import ckmysql
db = ckmysql( 'my_id', 'my_password', '127.0.0.1', 'my_db')
r = db.query("select * from test limit 5")
print(r) |