2017-02-03 90 views
0

我想用android studio中的SQLite數據庫編寫應用程序。數據庫表的一列應該在微調器中讀出。但是,當我嘗試啓動微調活動中,應用程序崩潰,並出現以下錯誤:android studio與sqlite錯誤

Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (near "FROM": syntax error (code 1): , while compiling: SELECT Gruppe , FROM Kunden_list)

這裏我類之一的代碼。我認爲錯誤是在這個班上,還是我錯了?

package com.example.katjarummler.hundeschule_petra_bennemann; 



import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

import java.util.ArrayList; 
import java.util.List; 

public class HundeschuleMemoDBHelper extends SQLiteOpenHelper { 


private static final String LOG_TAG =    
HundeschuleMemoDBHelper.class.getSimpleName(); 



public static final String DB_NAME = "Kunden_list.db"; 
public static final int DB_VERSION = 6; 

public static final String TABLE_KUNDEN_LIST = "Kunden_list"; 


public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_KName = "Name"; 
public static final String COLUMN_GNAME = "Gruppe"; 
public static final String COLUMN_KTELEFON = "Telefon"; 
public static final String COLUMN_HUND = "Hund"; 
public static final String COLUMN_BEGINN = "Beginn"; 
public static final String COLUMN_CHECKED = "checked"; 



public static final String SQL_CREATE = "CREATE TABLE " + 
TABLE_KUNDEN_LIST + 
     "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     COLUMN_KName + " TEXT NOT NULL, " + 
     COLUMN_GNAME + " TEXT NOT NULL, " + 
     COLUMN_KTELEFON + " TEXT NOT NULL, " + 
     COLUMN_HUND + " TEXT NOT NULL, " + 
     COLUMN_BEGINN + " TEXT NOT NULL, " + 
     COLUMN_CHECKED + " BOOLEAN NOT NULL DEFAULT 0);"; 


public static final String SQL_DROP = "DROP TABLE IF EXISTS " + 
TABLE_KUNDEN_LIST; 



public HundeschuleMemoDBHelper(Context context) { 
    super(context, DB_NAME, null, DB_VERSION); 
    Log.d(LOG_TAG, "DBHelper hat die Datenbank: " + getDatabaseName() + 
" erzeugt."); 
} 



//Die onCreate-Methode wird nur aufgerufen, falls die Datenbank noch 
nicht existiert 
public void onCreate(SQLiteDatabase db) { 
    try{ 
     Log.d(LOG_TAG, "Die Tabelle wird mit SQL-Befehl: " + SQL_CREATE 
+ " angelegt."); 
     db.execSQL(SQL_CREATE); 

    } 
    catch(Exception ex){ 
     Log.e(LOG_TAG, "Fehler beim Anlegen der Tabelle: " +  
ex.getMessage()); 
    } 

} 


// Die onUpgrade-Methode wird aufgerufen, sobald die neue 
Versionsnummer höher 
// als die alte Versionsnummer ist und somit ein Upgrade notwendig wird 

public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
newVersion) { 
    Log.d(LOG_TAG, "Die Tabelle mit Versionsnummer " + oldVersion + " 
wird entfernt."); 
    db.execSQL(SQL_DROP); 

    onCreate(db); 
} 


public List<HundeschuleMemoKunden> getAll(){ 

    SQLiteDatabase database = this.getReadableDatabase(); 
    String selectQuery = "SELECT " + 
      COLUMN_GNAME + " , " + 
      " FROM " + TABLE_KUNDEN_LIST; 

    List<HundeschuleMemoKunden> gruppenList = new 
ArrayList<HundeschuleMemoKunden>(); 
    Cursor cursor = database.rawQuery(selectQuery, null); //The mark 
of the error is here 

    //looping through all rows and adding to list 
    if(cursor.moveToFirst()){ 
     do{ 

      HundeschuleMemoKunden gruppe = new 
HundeschuleMemoKunden("kName", "gName", "kTelefon", "hName", "beginn"); 

gruppe.setgName(cursor.getString(cursor.getColumnIndex 
(this.COLUMN_GNAME)) 
); 
      gruppenList.add(gruppe); 

     }while(cursor.moveToNext()); 
    } 

    cursor.close(); 
    database.close(); 
    return gruppenList; 
} 



} 
+0

您的選擇查詢錯誤。你最有可能錯過'',''''之前的'''''FROM'之後的一列。 – user8

回答

1

錯誤是在你GETALL()方法,通過異常詳細說明:

String selectQuery = "SELECT " + 
     COLUMN_GNAME + " , " + 
     " FROM " + TABLE_KUNDEN_LIST; 

語法錯誤在你的SELECT查詢,逗號旁邊FROM。更改爲:

String selectQuery = "SELECT " + 
    COLUMN_GNAME + 
    " FROM " + TABLE_KUNDEN_LIST; 
0
public static final String SQL_CREATE = "CREATE TABLE " + 
TABLE_KUNDEN_LIST + 
    "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    COLUMN_KName + " TEXT NOT NULL, " + 
    COLUMN_GNAME + " TEXT NOT NULL, " + 
    COLUMN_KTELEFON + " TEXT NOT NULL, " + 
    COLUMN_HUND + " TEXT NOT NULL, " + 
    COLUMN_BEGINN + " TEXT NOT NULL, " + 
    COLUMN_CHECKED + " BOOLEAN NOT NULL DEFAULT 0);"; 

布爾不存在sqlite的,它應該是一個整數列,其中解析是:

//value is 1 then true else false 
value > 0 ? true : false 

和插入應該

//value is true = 1 else 0 
value ? 1 : 0 

如果這個數據是準確的,那麼表格就不能正確創建。 也從您的select語句中刪除逗號。也不要做原始查詢,你應該熟練掌握database.query,database.insert,database.delete等等。