-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path数据库使用示例.txt
74 lines (50 loc) · 1.3 KB
/
数据库使用示例.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//数据实体定义规范
class User : public DbEntity
{
public:
string m_name;
int m_age;
public:
void bindMember()
{
bindTableName("user");
bindFeild("name", &m_name);
bindFeild("age", &m_age);
}
};
获取数据对象:
DbInfo dbInfo;
dbInfo.m_host = "127.0.0.1";
dbInfo.m_port = 3306;
dbInfo.m_userName = "root";
dbInfo.m_password = "123456";
dbInfo.m_dbName = "test";
MysqlDb testDb(dbInfo);
testDb.link();
deque<User> userList;
testDb.getObjectList(userList, "select * from user");
for (User& user : userList)
{
cout << user.m_name << " " << user.m_age << endl;
}
插入数据对象
DbInfo dbInfo;
dbInfo.m_host = "127.0.0.1";
dbInfo.m_port = 3306;
dbInfo.m_userName = "root";
dbInfo.m_password = "123456";
dbInfo.m_dbName = "test";
MysqlDb testDb(dbInfo);
testDb.link();
User newUser;
newUser.m_name = "test";
newUser.m_age = 10;
testDb.insertObject(newUser);
SqlString的使用:
SqlString sql("select * from user where id between ? and ?");
sql.orderlyReplace("002");
sql.orderlyReplace("004");
cout << sql.toString() << endl;
说明:
1 复杂sql的查询使用 getHandle() 拿到实例,手动遍历数据。(后期会添加一个结果集的类。) 小心多线程陷阱。
2 有一个 SqlString 类可以帮助写sql语句。