2014-11-27 11 views
5
 
    11-27 03:32:04.471: E/AndroidRuntime(23137): Caused by: android.database.sqlite.SQLiteException: near "order": syntax error (code 1): , while compiling: create table order (_id integer primary key autoincrement, origin text not null, quantity integer not null);

klasa MyDatabase: KlasaSpowodowany przez: android.database.sqlite.SQLiteException: w pobliżu "": błąd składni (kod 1), podczas kompilacji:

public class MyDatabase extends SQLiteOpenHelper { 

    public static final String TABLE_NAME = "order"; 
    public static final String TABLE_ID = "_id"; 
    public static final String TABLE_ORIGIN = "origin"; 
    public static final String TABLE_QUANTITY = "quantity"; 
    private static final String DATABASE_NAME = "Appple.db"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table " + TABLE_NAME + 
      "(" + TABLE_ID + " integer primary key autoincrement, " + 
      TABLE_ORIGIN + " text not null, " + 
      TABLE_QUANTITY + " integer not null);"; 

    public MyDatabase (Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(DATABASE_CREATE); 
    } 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 
} 

Operation:

public class Operation { 

    private MyDatabase dbHelper; 
    private String[] COLUMNS = { MyDatabase.TABLE_ID, MyDatabase.TABLE_ORIGIN, MyDatabase.TABLE_QUANTITY }; 
    private SQLiteDatabase database; 


    public Operation(Context context) { 
     dbHelper = new MyDatabase(context); 
    } 

    public void open() throws SQLException { 
     database = dbHelper.getWritableDatabase(); 
    } 

    public void close() { 
     dbHelper.close(); 
    } 

    public void add(String origin, String quantity) { 
     ContentValues values = new ContentValues(); 
     values.put(MyDatabase.TABLE_ORIGIN, origin); 
     values.put(MyDatabase.TABLE_QUANTITY, Integer.parseInt(quantity)); 
     database.insert(MyDatabase.TABLE_NAME, null, values); 
    }  

    public int get(String origin) { 
     int total = 0; 

     Cursor cursor = database.query(MyDatabase.TABLE_NAME, COLUMNS, 
       MyDatabase.TABLE_ORIGIN + " = " + origin, null, null, null, null); 
     cursor.moveToFirst(); 
     while(!cursor.isAfterLast()) { 
      total += cursor.getInt(2); 
      cursor.moveToNext(); 
     } 
     cursor.close(); 
     return total; 
    } 
} 

W główną działalność rozpocząć

Operation op; 
op = new Operation(this); 
op.open(); 

myślę, że nie jest problemem w CREATE TABLE. Ale nie mogę znaleźć przyczyny błędu.

+0

Powinieneś być bardziej przejrzysty z tym, jaki masz problem. Zmień tytuł pytania na bardziej odpowiedni i podaj dokładny problem. Uwzględnij także pełny stacktrace i określ dokładną linię, w której on występuje. – Magnilex

Odpowiedz

11

order jest słowem kluczowym w języku SQL. Zmień nazwę tabeli lub umieść nazwę tabeli w podwójnych cudzysłowach, takich jak "order".

+0

Czołgi! :) Spędzam dużo czasu, aby go rozwiązać. – user4299176

+0

człowiek tnx. to bardzo oszczędzało mój czas. –

6

order to słowo kluczowe sqlite, a słowa kluczowe sqlite nie mogą być używane jako nazwa tabeli. Here można znaleźć listę tych

+0

Tannk ci bardzo! Miłego dnia! – user4299176

+0

Mam ten sam błąd dla definicji nazwy kolumny. Więc nie używaj nazw z listy słów kluczowych sqlite. – vovahost

+0

@vovahost Trudno powiedzieć, co się dzieje, nie widząc kodu. Spróbuj zadać pytanie – Blackbelt

Powiązane problemy