A MySQL cloud library
Project description
zx_mysql
zx_mysql 是一个用于连接和操作 MySQL 数据库的 Python 库。
安装
pip install zx_db_manager
from zx_db_manager import MySqlCloud
DEFAULT_HOST = '120.48.151.191'
DEFAULT_PORT = 3306
DEFAULT_USER = 'zxtech'
DEFAULT_PASSWORD = os.getenv('SUPER_PASSWORD')
# 修正:添加必要的初始化参数
test = MySqlCloud(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_USER, DEFAULT_PASSWORD)
# 连接数据库
def test_query():
# 建立数据库连接,假设数据库名为 'your_database_name',请根据实际情况修改
database_name = 'requirement'
connection = test.connect_database(database_name)
result = []
if connection:
# 修改查询语句,添加 LIMIT 100
query = "SELECT custom_name, title, sub_system FROM requirement LIMIT 100"
rows = test.execute_query(query)
if rows:
print("查询结果:")
for row in rows:
print(row)
result = rows
else:
print("未查询到数据。")
else:
print("数据库连接失败,请检查配置信息。")
return result
def create_test_database():
"""创建 test 数据库"""
database_name = 'test'
if test.create_database(database_name):
print(f"成功创建数据库 {database_name}")
else:
print(f"创建数据库 {database_name} 失败")
def create_test_table():
"""在 test 数据库中创建表"""
database_name = 'test'
table_name = 'test_table'
# 修改表结构,使其与查询字段同步
table_schema = "custom_name VARCHAR(255), title VARCHAR(255), sub_system VARCHAR(255)"
if test.create_table(database_name, table_name, table_schema):
print(f"成功在 {database_name} 中创建表 {table_name}")
else:
print(f"在 {database_name} 中创建表 {table_name} 失败")
def insert_test_data():
"""向 test_table 表中插入来自 test_query 的数据"""
database_name = 'test'
table_name = 'test_table'
insert_query = f"INSERT INTO {table_name} (custom_name, title, sub_system) VALUES (%s, %s, %s)"
data_rows = test_query()
connection = test.connect_database(database_name)
if connection and data_rows:
for data in data_rows:
test.execute_insert(insert_query, data)
print(f"成功向 {table_name} 插入 {len(data_rows)} 条数据")
else:
if not connection:
print(f"连接数据库 {database_name} 失败,无法插入数据")
else:
print("没有可插入的数据。")
def test_execute_sql():
# 连接到数据库
database_name = 'test'
if test.connect_database(database_name):
try:
# 使用 test 数据库
use_db_sql = "USE test"
result = test.execute_sql(use_db_sql)
if result:
print("成功使用 test 数据库")
else:
print("使用 test 数据库失败")
# 删除 test_table 表中的所有数据
delete_sql = "DELETE FROM test_table"
result = test.execute_sql(delete_sql)
if result:
print("成功删除 test_table 表中的所有数据")
else:
print("删除 test_table 表数据失败")
except Exception as e:
print(f"执行 SQL 语句时出错: {e}")
finally:
# 关闭数据库连接
test.close_connection()
else:
print(f"无法连接到数据库 {database_name}")
if __name__ == "__main__":
test_query()
#test_database_test()
#test_execute_sql()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zx_db_manager-0.1.2.tar.gz
(4.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zx_db_manager-0.1.2.tar.gz.
File metadata
- Download URL: zx_db_manager-0.1.2.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b4b98916a43d2aee2422e84c3d4b0792fc141c14d4f2a8c88397d957939cd73
|
|
| MD5 |
ac558c31591ae812115902af106bee3e
|
|
| BLAKE2b-256 |
aaadadf1aac878542df69cc93cdfcd03cb589443267c825083106c9b14229741
|
File details
Details for the file zx_db_manager-0.1.2-py3-none-any.whl.
File metadata
- Download URL: zx_db_manager-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ff0061b177309c629dc46fa3662444354e2ddf10f7e36fa749ed4358e0dad05
|
|
| MD5 |
029786288de0b5a0e777358e3892912a
|
|
| BLAKE2b-256 |
2fccfced19266e1b47d06af43121822c2c35eca68f3c8cc34584013eb24b68c9
|