查询在sqlite db浏览器中工作但不在android中

时间:2016-04-14 02:37:29

标签: android sqlite android-sqlite

我的数据库有两个表 - BundlesClaim_records。捆绑包有列bundle_idbundle_date。捆绑包可以有多个索赔记录。因此,每个claim_record都拥有一个bundle_id,它与Bundles表中的记录相关联。

为了使用bundle填充listview,我需要创建一个sqlite视图,其中包含bundle_idbundle_date,声明数量以及每个捆绑包的索​​赔总额。我在sqlite数据库浏览器中运行了下面的sqlite查询,它给了我所需的结果。但是我一直在

  

“(”:语法错误(代码1):

附近的异常)

以下是我尝试运行的查询:

CREATE VIEW view_bundle_details ( bid, bdate, num_of_claims, total_charge_claims ) 
  AS SELECT bundles.bundle_id ,bundle_date ,COUNT ( * ) , SUM ( claim_records.charge )

这里的图像是在sqlitebrowser中运行的查询以及我想要实现的结果

enter image description here

这是Bundles类,我正在运行的方法是getCreateBundleDetailsView();

public class Bundles extends DataClass {
public static final String TABLE_NAME_BUNDLES_RECORDS = "bundles";
public static final String BUNDLE_ID = "bundle_id";
public static final String BUNDLE_DATE = "bundle_date";
public static final String NUM_OF_CLAIMS = "num_of_claims";
public static final String BUNDLE_CHARGE = "total_charge_claims";
public static final String VIEW_NAME_BUNDLE_DETAILS = "view_bundle_details";
public static final String VIEW_BUNDLE_ID = "bid";
public static final String VIEW_BUNDLE_DATE = "bdate";

private int idAfterInsertion = 0;
private String[] columns={BUNDLE_ID,BUNDLE_DATE,BUNDLE_CHARGE };
/**
 * Creates an object of DataClass and calls getWritableDatabase to force database creation if necessary
 *
 * @param context
 */
public Bundles(Context context) {
    super(context);
}

public static String getCreateSQLString() {
    return "create table " + TABLE_NAME_BUNDLES_RECORDS + " ("
            + BUNDLE_ID + " integer primary key, "
            + BUNDLE_DATE + " text default '1900-01-01', "
            + BUNDLE_CHARGE + " real default 0"
            + " )";
}

public static  String getCreateBundleDetailsView(){
    return "CREATE VIEW " + VIEW_NAME_BUNDLE_DETAILS + " ( "
            + VIEW_BUNDLE_ID +", "+ VIEW_BUNDLE_DATE +", "+ NUM_OF_CLAIMS +", "+ BUNDLE_CHARGE+ " ) "
            + "AS SELECT " + TABLE_NAME_BUNDLES_RECORDS + "." +BUNDLE_ID +" ,"+BUNDLE_DATE
            + " ,COUNT ( * ) , SUM ( " + ClaimRecords.TABLE_NAME_CLAIM_RECORDS+"."
            + ClaimRecords.CLAIM_CHARGE +" ) FROM "+TABLE_NAME_BUNDLES_RECORDS+" LEFT "
            + "JOIN "+ClaimRecords.TABLE_NAME_CLAIM_RECORDS+" ON "+TABLE_NAME_BUNDLES_RECORDS+"."
            + BUNDLE_ID + " = "+ClaimRecords.TABLE_NAME_CLAIM_RECORDS+"."+BUNDLE_ID + " GROUP BY "
            + TABLE_NAME_BUNDLES_RECORDS+"."+BUNDLE_ID;
}


public boolean addOrUpdate(String bundleDate) {
    try {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();


        cv.put(BUNDLE_DATE, bundleDate);
        idAfterInsertion = (int)db.insertWithOnConflict(TABLE_NAME_BUNDLES_RECORDS, null, cv, SQLiteDatabase.CONFLICT_REPLACE);
        if (idAfterInsertion<= 0) {
            return false;
        }
        close();
        return true;
    } catch (Exception ex) {
        close();
        return false;
    }
}
public int getJustAddedId(){
    return idAfterInsertion;

}
public int getBundleID(String bundleDate) {
    try {
        String selection = BUNDLE_DATE + "=" + bundleDate;
        cursor = db.query(TABLE_NAME_BUNDLES_RECORDS, columns, selection, null, null, null, null);
        cursor.moveToFirst();
        Bundle obj = fetch();
        close();
        return obj.getId();
    } catch (Exception ex) {
        Log.d("OPDCases.getOPDCase(int)", "Exception " + ex.getMessage());
        return 0;
    }
}


public Bundle fetch(){
    try
    {

        if(cursor.isBeforeFirst()){
            cursor.moveToFirst();
        }
        int index=cursor.getColumnIndex(BUNDLE_ID);
        int id=cursor.getInt(index);
        index=cursor.getColumnIndex(BUNDLE_DATE);
        String name=cursor.getString(index);
        index=cursor.getColumnIndex(BUNDLE_CHARGE);
        float charge=cursor.getFloat(index);
        cursor.moveToNext();
        return new Bundle(id,name,charge);
    }catch(Exception ex){
        return null;
    }

}

public Bundle fetchForBundleReport(){
    try
    {

        if(cursor.isBeforeFirst()){
            cursor.moveToFirst();
        }
        int index=cursor.getColumnIndex(VIEW_BUNDLE_ID);
        int id=cursor.getInt(index);
        index=cursor.getColumnIndex(VIEW_BUNDLE_DATE);
        String name=cursor.getString(index);
        index=cursor.getColumnIndex(NUM_OF_CLAIMS);
        int numOfClaims=cursor.getInt(index);
        index=cursor.getColumnIndex(BUNDLE_CHARGE);
        float charge=cursor.getFloat(index);
        cursor.moveToNext();
        return new Bundle(id,name,charge,numOfClaims);
    }catch(Exception ex){
        return null;
    }

}


public ArrayList<Bundle> getBundles(){
    ArrayList<Bundle> list=new ArrayList<Bundle>();
    try
    {
        db=getReadableDatabase();
        cursor=db.query(TABLE_NAME_BUNDLES_RECORDS, columns, null, null, null, null, null);
        cursor.moveToFirst();
        Bundle obj=fetch();
        while(obj!=null){
            list.add(obj);
            obj=fetch();
        }
        close();
        return list;
    }catch(Exception ex){
        close();
        return list;
    }
}

public ArrayList<Bundle> getBundleReport(){
    ArrayList<Bundle> list=new ArrayList<Bundle>();
    try
    {
        db=getReadableDatabase();
        String[] columns={VIEW_BUNDLE_ID,VIEW_BUNDLE_DATE,NUM_OF_CLAIMS,BUNDLE_CHARGE};
        cursor=db.query(VIEW_NAME_BUNDLE_DETAILS, columns, null, null, null, null, null);
        cursor.moveToFirst();
        Bundle obj=fetchForBundleReport();
        while(obj!=null){
            list.add(obj);
            obj=fetchForBundleReport();
        }
        close();
        return list;
    }catch(Exception ex){
        close();
        return list;
    }
}

}

这是logcat

  

04-14 02:31:27.416 6928-6928 / com.ashesi.cs.mhealth.data E / SQLiteLog:(1)near“(”:syntax error

     

04-14 02:31:27.416 6928-6928 / com.ashesi.cs.mhealth.data E / DataClass.onCreate:“(”:语法错误(代码1)附近的异常:,编译时:CREATE VIEW view_bundle_details (bid,bdate,num_of_claims,total_charge_claims)AS SELECT bundles.bundle_id,bundle_date,COUNT(*),SUM(claim_records.charge)FROM bundle LEFT JOIN claim_records ON bundles.bundle_id = claim_records.bundle_id GROUP BY bundles.bundle_id

     

04-14 02:31:27.496 6928-6928 / com.ashesi.cs.mhealth.data D / libEGL:loaded /system/lib/egl/libEGL_MRVL.so

     

04-14 02:31:27.506 6928-6928 / com.ashesi.cs.mhealth.data D / libEGL:loaded /system/lib/egl/libGLESv1_CM_MRVL.so

     

04-14 02:31:27.546 6928-6928 / com.ashesi.cs.mhealth.data D / libEGL:loaded /system/lib/egl/libGLESv2_MRVL.so

     

04-14 02:31:27.586 6928-6928 / com.ashesi.cs.mhealth.data D / GC:OES20 ===&gt; GC版本:GC Ver rls_pxa988_KK44_GC13.20

     

04-14 02:31:27.606 6928-6928 / com.ashesi.cs.mhealth.data D / OpenGLRenderer:启用调试模式0

     

04-14 02:38:50.909 6928-6934 / com.ashesi.cs.mhealth.data D / dalvikvm:调试器已分离;对象注册表有1个条目

     

04-14 02:39:02.490 6928-6928 / com.ashesi.cs.mhealth.data W / IInputConnectionWrapper:showStatusIcon on inactive InputConnection

1 个答案:

答案 0 :(得分:0)

您需要执行以下语句

CREATE VIEW view_bundle_details AS SELECT bundles.bundle_id AS bid ,bundle_date AS bdate ,COUNT ( *
 ) AS num_of_claims, SUM ( claim_records.charge ) AS total_charge_claims FROM bundles LEFT JOIN claim_records 
ON bundles.bundle_id = claim_records.bundle_id GROUP BY 
bundles.bundle_id

根据this

如果列名称列表跟在视图名称后面,那么该列表将确定视图列的名称。如果省略列名列表,则视图中列的名称将从select-stmt中的结果集列的名称派生。请注意,仅在SQLite版本3.9.0及更高版本中支持列名列表语法。

所以我相信这就是为什么它会给你一个语法错误。请试试这个。