测试总结
对于mud数据库的开发单个字段的查询返回单个数据,多个字段应拼接成mapping。 数据的插入和更新组成接口,将mapping解析成语句即可。 数据库使用可以保持长连接,如想使用多数据库在不同代码平台上开发,可以在master主控程序上封装自己的配置文件。当配置文件改动时,使用的数据库一同改动。
使用数据库的好处
当多个数据库串联起来时,有些数据和功能就不用使用lpc去处理了,因为本质上玩家的属性只是数据,做好数据在不同平台的传输和处理,就能在其他代码平台上开发更强大的功能
#include <mysql.h>
#include <ansi.h>
private mixed myDb, line;
mixed obtain_message(int i);
private mixed implode_sql_value(mixed *sqlArray,string del);
//当精灵启动时,创建数据库长连接
void create()
{
seteuid(getuid());
// set("channel_id", "数据库精灵");
myDb = db_connect(DB_HOST, DB_NAME, DB_USER, __USE_MYSQL__);
}
//数据库执行sql语句。
mixed exec_sentence(string sentence)
{
line = db_exec(myDb, sentence);
return line;
}
//字段值的连接
mixed implode_sql_value(mixed *sqlArray,string del)
{
string resultMsg = "";
foreach (mixed value in sqlArray)
{
switch (typeof(value))
{
case "int":
case "float":
resultMsg += value + del;
break;
default:
resultMsg += "'" + value + "'" + del;
break;
}
}
return resultMsg[0.. < sizeof(del) + 1];
}
//支持单查询,多查询,约束查询
mixed query_data(string id, string tableName, mixed queryBody, int all, mapping where)
{
string sqlSentence;
int type = 0;
if (!all)
{
if (arrayp(queryBody))
{
sqlSentence = sprintf("select %s from %s where id='%s';", implode(queryBody, ","), tableName, id);
}
else if (stringp(queryBody))
{
if (queryBody != "*")
type = 1;
sqlSentence = sprintf("select %s from %s where id='%s';", queryBody, tableName, id);
}
}
else
{
string *constraint = keys(where);
foreach (string key in constraint)
{
key += sprintf("='%s'",where[key]);
}
if (arrayp(queryBody))
{
sqlSentence = sprintf("select %s from %s where id='%s',;", implode(queryBody, ","), tableName, id, implode(constraint, ","));
}
else if (stringp(queryBody))
{
if (queryBody != "*")
type = 1;
sqlSentence = sprintf("select %s from %s where id='%s',;", queryBody, tableName, id, implode(constraint, ","));
}
}
exec_sentence(sqlSentence);
return obtain_message(type);
}
//插入数据
mixed insert_data(string tableName,mapping data)
{
string sqlSentence;
mixed *constraint = values(data);
mixed valued;
valued = implode_sql_value(constraint,",");
sqlSentence = sprintf("insert into %s (%s) values (%s);",tableName,implode(keys(data),","),valued);
return exec_sentence(sqlSentence);
}
//更新数据
mixed update_data(string id,string tableName,mapping data)
{
string sqlSentence;
mixed *constraint = keys(data),*valued = ({});
foreach (string key in constraint)
{
valued += ({ key + "='" + data[key] + "'" });
}
sqlSentence = sprintf("update %s set %s where id='%s';",tableName,implode(valued,","),id);
return exec_sentence(sqlSentence);
}
//关闭数据库连接
mixed close()
{
return db_close(myDb);
}
//重新连接数据库
mixed connect()
{
myDb = db_connect(DB_HOST, DB_NAME, DB_USER, __USE_MYSQL__);
}
//获取上一次数据库返回其中的信息,可以指定行,如果不指定行,传回所有数据 ---- 0为多查询,1为单查询
mixed obtain_message(int i)
{
mixed res;
mixed fileds;
if (undefinedp(line) || !line)
{
return 0;
}
if (stringp(line))
{
return line;
}
if (i == 0)
{
mapping data = ([]);
fileds = db_fetch(myDb, 0);
for (i = 1; i <= line; i++)
{
res = db_fetch(myDb, i);
data = allocate_mapping(fileds, res); //以主键作为key返回哈希表
}
return data;
}
res = db_fetch(myDb, i);
if (arrayp(res) && sizeof(res) == 1)
{
res = res[0];
}
return res;
}
int obtain_line()
{
return line;
}