2022-01-05(数据库)
2022-01-05 本文已影响0人
ImmortalSummer
如何查看和操作真机运行的数据库
查看当前设备,下载当前app的container,然后查看其包内容,按照数据库的存储位置,可以找到对应的sqlite文件,然后用Navicat 链接该数据库文件即可查看。
https://blog.csdn.net/wuzesong/article/details/51324324


如何查找前5条数据
网络上找到的 select top 5 * from table_name
是不起作用的,报语法错误,因为这是SQL的语法。
在MySQL、Sqlite中的语法是:select * from table_name limit 0,5
https://www.cnblogs.com/xkms/p/4664473.html
按照时间排序,只保留前5条数据,其他的都删除
delete from table_name where id not in (select id from table_name where uid = 666 order by timestamp desc limit 0,5)
FMDB中的增删改查
// 删除数据
NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE uid = %@ and targetid = %@", tableName, uid, targetid];
[db executeUpdate:deleteSQL];
// 插入数据
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO %@ ('uid','targetid','timestamp') VALUES (?,?,?)", tableName];
[db executeUpdate:insertSQL, uid, targetid, timestamp];
// 更新数据
NSString *updateSQL = [NSString stringWithFormat:@"UPDATE %@ SET timestamp=?, content=? WHERE uid=%@ and targetid=%@", tableName, uid, targetid];
[db executeUpdate:updateSQL, timestamp, content];
// 查看数据(按时间降序排序)
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ where uid = %ld ORDER BY timestamp DESC", tableName, uid];
FMResultSet *rs = [db executeQuery:sql];
while ([rs next]) {
[rs objectForColumn:@"targetid"];
[rs objectForColumn:@"timestamp"];
}