查询所有
from mysql.db_object import Database
db = Database(host="localhost",
password="zhangdapeng520",
database="zdppy_mysql_demo")
table = "test_user"
sql = """
create table test_user(
id bigint primary key auto_increment,
name varchar(255)
);
"""
db.delete_table(table)
db.execute(sql)
print(db.get_all_table())
db.add(table, ["name"], ["张三"])
db.add(table, ["name"], ["李四"])
db.add(table, ["name"], ["王五"])
print(db.get_all(table))
db.delete_table(table)
print(db.get_all_table())
根据ID查询
from mysql.db_object import Database
db = Database(host="localhost",
password="zhangdapeng520",
database="zdppy_mysql_demo")
table = "test_user"
sql = """
create table test_user(
id bigint primary key auto_increment,
name varchar(255)
);
"""
db.delete_table(table)
db.execute(sql)
print(db.get_all_table())
db.add(table, ["name"], ["张三"])
db.add(table, ["name"], ["李四"])
db.add(table, ["name"], ["王五"])
print(db.get_by_id(table, 1))
db.delete_table(table)
print(db.get_all_table())
根据ID列表查询
from mysql.db_object import Database
db = Database(host="localhost",
password="zhangdapeng520",
database="zdppy_mysql_demo")
table = "test_user"
sql = """
create table test_user(
id bigint primary key auto_increment,
name varchar(255)
);
"""
db.delete_table(table)
db.execute(sql)
print(db.get_all_table())
db.add(table, ["name"], ["张三"])
db.add(table, ["name"], ["李四"])
db.add(table, ["name"], ["王五"])
print(db.get_by_ids(table, [1, 2, 3]))
db.delete_table(table)
print(db.get_all_table())
去重查询
from mysql.db_object import Database
db = Database(password="zhangdapeng520", database="zdppy_mysql_demo")
table = "test_user"
sql = """
create table test_user(
id int primary key auto_increment,
name varchar(36)
)
"""
db.delete_table(table)
db.execute(sql)
db.add(table, ["name"], ["a"])
db.add(table, ["name"], ["a"])
db.add(table, ["name"], ["a"])
db.add(table, ["name"], ["b"])
db.add(table, ["name"], ["b"])
db.add(table, ["name"], ["c"])
print(db.get_all(table))
print(db.get_all_distinct(table, "name"))