共计 3375 个字符,预计需要花费 9 分钟才能阅读完成。
CouchBase-lite 是 Couchbase 官网出的一个嵌入式文档数据库,专门编码格调是用于挪动设施应用程序的,其中就有 java 版本。其底层应用的是 sqlite 数据库
以下是我首次踩坑编写的代码
import java.time.LocalDate;
import com.couchbase.lite.CouchbaseLite;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.DataSource;
import com.couchbase.lite.Database;
import com.couchbase.lite.DatabaseConfiguration;
import com.couchbase.lite.Dictionary;
import com.couchbase.lite.Expression;
import com.couchbase.lite.Meta;
import com.couchbase.lite.MutableDocument;
import com.couchbase.lite.Query;
import com.couchbase.lite.QueryBuilder;
import com.couchbase.lite.Result;
import com.couchbase.lite.ResultSet;
import com.couchbase.lite.SelectResult;
public class Test {
public static String path = "path";
public static String tableName="db";
public static void main(String[] args) throws CouchbaseLiteException {
// TODO Auto-generated method stub
// 初始化 CouchbaseLite 模块?CouchbaseLite.init();
// 配置对象实例?DatabaseConfiguration config = new DatabaseConfiguration();
// 设置 db 文件门路
config.setDirectory(path);
try {//query(config);
testDelete(config);
} catch (CouchbaseLiteException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}
public void read(DatabaseConfiguration config) {for(int i=1;i<10;i++) {new Thread(()->{
try {query(config);
} catch (CouchbaseLiteException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}).start();}
}
// 关上保留
public static void testAdd(DatabaseConfiguration config) throws CouchbaseLiteException {
Database database = null;
try {database = new Database(tableName, config);
MutableDocument doc = new MutableDocument().setString("sad", "dsf").setString("aa", "bb")
.setValue("date_test", LocalDate.now().toString());
database.save(doc);
System.out.println("id:" + doc.getId());
} catch (CouchbaseLiteException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}
public static void testDelete(DatabaseConfiguration config) throws CouchbaseLiteException {
Database database = null;
try {database = new Database(tableName, config);
Query query = QueryBuilder
.select(SelectResult.all(),
SelectResult.expression(Meta.id)
).
from(DataSource.database(database)).limit(Expression.intValue(1));
ResultSet rs=query.execute();
Result r=rs.next();
if(r!=null) {String id=r.getString("id") ;
System.out.println("id:"+id);
Dictionary dict = r.getDictionary(0);
for (String key : dict.getKeys()) {System.out.println(key + "=" + dict.getValue(key));
}
// 以下代码运行会抛出异样
//database.delete(new MutableDocument().setString("id", id));
// 删除一个汇合表
//database.delete();}
//System.out.println("id:" + doc.getId());
} catch (CouchbaseLiteException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}
public static void query(Database database) throws CouchbaseLiteException {Query query = QueryBuilder.select(SelectResult.all()).from(DataSource.database(database));
for (Result r : query.execute()) {System.out.println("\ncount:" + r.count());
Dictionary dict = r.getDictionary(0);
for (String key : dict.getKeys()) {System.out.println(key + "=" + dict.getValue(key));
}
}
}
public static void query(DatabaseConfiguration config) throws CouchbaseLiteException {
Database database = null;
database = new Database(tableName, config);
Query query = QueryBuilder
.select(SelectResult.all(),
SelectResult.expression(Meta.id).as("id")
).
from(DataSource.database(database));
for (Result r : query.execute().allResults()) {System.out.println("id"+":"+r.getString("id") );
Dictionary dict = r.getDictionary(0);
if(dict!=null)
for (String key : dict.getKeys()) {System.out.println(key + "=" + dict.getValue(key));
}
System.out.println();}
}
}
它的 delete 办法的 api 在非挪动设施上应用是有问题的,我感觉是偏差于挪动设施,无奈纯 id 删除内容。也可能是我没找到办法,如果有晓得的,在这里说以下
正文完
发表至: javascript
2021-06-24