快半年没更新博客了,发表篇文章 不能荒废了。 之前坚持了两年写博客的 中断了的话 有点可惜。
这个类不是线程安全版本请注意了 只是单实例
先看下目录结构 源代码打包放附件中
|-- Makefile
|-- include
| |-- mysql_conn.h
| `-- sns_concern.h
|-- lib
| |-- Db
| | |-- Makefile
| | |-- mysql_conn.cpp
| | `-- mysql_conn.o
| |-- Makefile
| |-- sns_concern.cpp
| `-- sns_concern.o
|-- ma2
|-- ma2.cpp
|-- ma2.o
`-- maketest
是一个完整的demo,makefile写的感觉有点挫。
现在贴出mysql_conn.cpp 和 实际使用它的文件 sns_concern.cpp
mysql_conn.cpp
#include <string> #include <iostream> #include <mysql++.h> #include "mysql_conn.h" using namespace std; using namespace mysqlpp; mysql_conn * mysql_conn::instance = NULL; mysql_conn * mysql_conn::get_instance() { char *user = "poker_wrt"; char *pwd = "poker_wrt"; char *host = "192.168.0.246"; char *db = "test"; char *charset = "gbk"; if (NULL == mysql_conn::instance) { instance = new mysql_conn(db, user, pwd, host, charset); } return instance; } mysql_conn::mysql_conn(char *db, char *username, char *password,char *host, char *charset) { if ( !con.connect(db, host, username, password)) { cout << "connect mysql error"<< endl; } string str = "set names " ; str = str + charset; this->mysql_execute(str); this->mysql_execute("set autocommit=0"); } void mysql_conn::hello() { cout << "from hello" << endl; } StoreQueryResult mysql_conn::mysql_query(string query_str, SQLQueryParms params) { Query query = con.query(query_str); query.parse(); StoreQueryResult res = query.store(params) ; return res; } int mysql_conn::mysql_execute(string query_str,SQLQueryParms params) { Query query = con.query(query_str); query.parse(); query.execute(params); return query.affected_rows(); } int mysql_conn::get_insert_id() { Query query = con.query(); return query.insert_id(); } int mysql_conn::commit() { Transaction trans(con); trans.commit(); return 1; } int mysql_conn::rollback() { Transaction trans(con); trans.rollback(); return 1; }
sns_concern.cpp
#include <iostream> #include <string> #include <mysql++.h> #include "sns_concern.h" using namespace std; using namespace mysqlpp; int sns_concern::add(int uid, string username, int uid_by, string username_by) { string insert_str = "insert into t_user_concern \ (f_uid,f_username,f_concern_uid,f_concern_username) \ values(%0q,%1q,%2q,%3q)"; SQLQueryParms params2; int nums; params2 += uid; params2 += username; params2 += uid_by; params2 += username_by; mysql_conn *p1 = mysql_conn::get_instance(); nums = p1->mysql_execute(insert_str,params2); p1->commit(); return nums; } int sns_concern::del(int uid, int uid_by) { string del_str = "delete from t_user_concern where f_uid = %0q and f_concern_uid = %1q"; SQLQueryParms params2; int nums; params2 += uid; params2 += uid_by; mysql_conn *p1 = mysql_conn::get_instance(); nums = p1->mysql_execute(del_str,params2); p1->commit(); return nums; } StoreQueryResult sns_concern::get(int uid) { string select_str = "select f_uid uid,f_username username,f_concern_uid concern_uid,\ f_concern_username concern_username from t_user_concern\ where f_uid = %0q"; SQLQueryParms params2; params2 += uid; mysql_conn *p1 = mysql_conn::get_instance(); return p1->mysql_query(select_str,params2); }
使用的表的sql语句在这里:
CREATE TABLE `t_user_concern` (
`f_uid` int(10) unsigned NOT NULL COMMENT '用户id',
`f_username` varchar(255) NOT NULL COMMENT '用户名',
`f_concern_uid` int(10) unsigned NOT NULL COMMENT '用户id',
`f_concern_username` varchar(255) NOT NULL COMMENT '用户名',
`f_instime` datetime DEFAULT NULL COMMENT '插入时间',
`f_uptime` datetime DEFAULT NULL COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户关注表'