java的CouchBase-lite的初次踩坑

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和update的操作有问题。无法根据id进行处理一行

它的delete方法的api在非移动设备的环境上使用是有问题的,我感觉是偏向于移动设备,无法以纯id为参数来删除内容。也可能是我没找到方法,如果有知道的,在这里说以下。

如果有还知道java有哪些嵌入式文档数据库,请告诉一下产品名称。

你可能感兴趣的:(java的CouchBase-lite的初次踩坑)