sqlite3的故事

来源:互联网 发布:淘宝的网络广告方式 编辑:程序博客网 时间:2024/05/29 13:41

主要还是在数据库内容同步的时候遇到的问题,服务器端也不标识变化发生,那客户端用sqilite3,本来就是要实现目的,udpate不成功,我就用insert。

自然想到去拿那个影响的record数目。


但看来大家多推荐replace,这个replace是不是要知道原来sql的primarykey的值啊,可我偷懒,没设置,全用了一个自增长的id,这下。。。


因为数据库建表时没有指定primary key, 所以replace语句不正常.  


option 1 would look like:  //基本来说,目前的数据结构,表,应该可以做到用replace or insert。。。。。

NSString * update = [NSString stringWithFormat:@"INSERT OR REPLACE INTO LIST (COUNT, NAME, DATE, IDNUM) VALUES (?, ?, ?, ?)"];

option 2 would look like:  //至于效率问题,我们需要找个同学测试下。。。。

NSString * update = [NSString stringWithFormat:@"SELECT * FROM LIST WHERE IDNUM = '%i'", [accessID integerValue]];sqlite3_stmt *stmt;int rc = sqlite3_prepare_v2(db, [update UTF8String], -1, &stmt, nil);if (rc == SQLITE_OK) {    if (sqlite3_step(stmt) == SQLITE_ROW) {        update = [NSString stringWithFormat:@"UPDATE LIST SET COUNT = ?, NAME = ?, DATE = ?, IDNUM = ? WHERE IDNUM = '%i'", [accessID integerValue]];    }    else{        update = [NSString stringWithFormat:@"INSERT OR REPLACE INTO LIST (COUNT, NAME, DATE, IDNUM) VALUES (?, ?, ?, ?)"];       }}
先上下面的方案吧:)

为了减少网络流量,请问我可以只拿房间GUID和状态两个字段么,呵呵。。。。



The MIN/MAX optimization===============

Queries of the following forms will be optimized to run in logarithmic time assuming appropriate indices exist:

  SELECT MIN(x) FROM table;  SELECT MAX(x) FROM table;

In order for these optimizations to occur, they must appear in exactly the form shown above - changing only the name of the table and column.It is not permissible to add a WHERE clause or do any arithmetic on the result. The result set must contain a single column. The column in the MIN or MAX function must be an indexed column.

所以我们的错误就在

select max(dd) as x,min(dd)as y from dd where state=4//本来测试没有看where,后来用了where发现不对了。这。。。。!


 
原创粉丝点击