在表单中插入数据时获取SQlite异常

时间:2015-12-09 05:23:22

标签: android sqlite

我正在创建一个使用SQlite的数据库应用程序。当我在输入表单中输入所有细节并点击提交时,我的应用程序崩溃了,我收到的消息如SQlite异常和语法错误附近")"以及更多。我已经粘贴了一个logcat描述。如果有人能解决我的问题。这将非常有帮助。

12-09 09:47:02.700: E/AndroidRuntime(8563): FATAL EXCEPTION: main
12-09 09:47:02.700: E/AndroidRuntime(8563): android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE Vegetable(idt INTEGER PRIMARY KEY,vegtharakari TEXT,tharakariprice TEXT,tharakariweight TEXT,tharakariplace TEXT,)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1719)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1650)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at com.ons.veg.dbhelper.onCreate(dbhelper.java:77)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at com.ons.veg.dbhelper.addVegetables(dbhelper.java:96)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at com.ons.veg.Vegform.onClick(Vegform.java:152)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.view.View.performClick(View.java:4102)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.view.View$PerformClick.run(View.java:17085)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.os.Handler.handleCallback(Handler.java:615)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.os.Looper.loop(Looper.java:155)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at android.app.ActivityThread.main(ActivityThread.java:5520)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
12-09 09:47:02.700: E/AndroidRuntime(8563):     at dalvik.system.NativeStart.main(Native Method)
  

package com.ons.veg;

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class dbhelper extends SQLiteOpenHelper{

    public static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Bangalore";
    private static final String TABLE_VEGETABLE = "Vegetable";
    private static final String TABLE_FRUITS = "fruits";


    // Variable = value
    //Vegetable
    private static final String KEY_ID = "idt";
    private static final String KEY_VEGITEMS = "vegtharakari";
    private static final String KEY_PRICE = "tharakariprice";          // when you type key and control spacke it gives full name
    private static final String KEY_WEIGHT = "tharakariweight";
    private static final String KEY_PLACE = "tharakariplace";


    //Fruit
    private static final String KEYF_IDD = "Id";
    private static final String KEYF_FRUITITEMS = "FruitItems";
    private static final String KEYF_PRICE = "Price";
    private static final String KEYF_WEIGHT = "Weight";
    private static final String KEYF_PLACE = "Place";

    private static final String CREATE_TABLE_FRUITS =  "CREATE TABLE " 
            + TABLE_FRUITS
            + "("
            + KEYF_IDD
            + " INTEGER PRIMARY KEY," 
            + KEYF_FRUITITEMS 
            + " TEXT,"
            + KEYF_PRICE 
            + " TEXT,"
            + KEYF_WEIGHT
            + " TEXT," 
            + KEYF_PLACE
            + " TEXT," 
            + " )";


    private static final String CREATE_TABLE_VEGETABLE =  "CREATE TABLE " 
            + TABLE_VEGETABLE
            + "("
            + KEY_ID 
            + " INTEGER PRIMARY KEY," 
            + KEY_VEGITEMS 
            + " TEXT,"
            + KEY_PRICE 
            + " TEXT,"
            + KEY_WEIGHT
            + " TEXT," 
            + KEY_PLACE
            + " TEXT," 
            + " )";


    public dbhelper(Context context) {
        super(context,DATABASE_NAME , null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_TABLE_VEGETABLE);
        db.execSQL(CREATE_TABLE_FRUITS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub


        db.execSQL("DROP TABLE IF EXISTS" + TABLE_VEGETABLE );
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_FRUITS );

        onCreate(db);

    }

    public void addVegetables(ModelOne model) {                 


        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_VEGITEMS, model.getVegtharakari());
        values.put(KEY_PRICE, model.getTharakariprice());
        values.put(KEY_WEIGHT, model.getTharakariweight());
        values.put(KEY_PLACE, model.getTharakariplace());

        db.insert(TABLE_VEGETABLE, null, values);

        db.close();

    }

    public void addfruits(ModelTwo model1) {                 


        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues valuess = new ContentValues();
        valuess.put(KEYF_FRUITITEMS, model1.getFruitItems());
        valuess.put(KEYF_PRICE, model1.getPrice());
        valuess.put(KEYF_WEIGHT, model1.getWeight());
        valuess.put(KEYF_PLACE, model1.getPlace());

        db.insert(TABLE_FRUITS, null, valuess);

        db.close();

    }


    public int updatecontact (ModelOne godrej){

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_VEGITEMS, godrej.getVegtharakari());
        values.put(KEY_PRICE, godrej.getTharakariplace());
        values.put(KEY_WEIGHT, godrej.getTharakariweight());
        values.put(KEY_PLACE, godrej.getTharakariplace());

        // updating row
        return db.update(TABLE_VEGETABLE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(godrej.getIdt()) });
    }

    public int updatefruits (ModelTwo godrej1){

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEYF_FRUITITEMS, godrej1.getFruitItems());
        values.put(KEYF_PRICE, godrej1.getPrice());
        values.put(KEYF_WEIGHT, godrej1.getWeight());
        values.put(KEYF_PLACE, godrej1.getPlace());


        // updating row
        return db.update(TABLE_FRUITS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(godrej1.getId()) });
    }

    public void deletecontact (long id){

        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_VEGETABLE, KEY_ID + " = ?",
                new String[] { String.valueOf(id) });
        db.close();

    }

    public void deletefruits (long id){

        SQLiteDatabase db = this.getWritableDatabase();

        db.delete(TABLE_FRUITS, KEY_ID + " = ?",
                new String[] { String.valueOf(id) });
        db.close();

    }

 /**    private void printdatabase() {
        // TODO Auto-generated method stub

        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT  * FROM " + TABLE_VEGETABLE;
        Cursor cursor = db.rawQuery(selectQuery, null); 

        if (cursor.moveToFirst()){

            System.out.println("Vegetable" + cursor.getString(0));
            System.out.println("Price" + cursor.getString(1));
            System.out.println("Weight" + cursor.getString(2));
            System.out.println("Place" + cursor.getString(3));
        }

        This method is for printing and checking in the logcat

    } **/


    public List<ModelOne> getAllContacts() {
        List<ModelOne> contactListveg = new ArrayList<ModelOne>();  // Array list created to hold the data from database ( rows).
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_VEGETABLE;

        SQLiteDatabase db = this.getWritableDatabase(); //this is requesting database to allow to write some data to it.
        Cursor cursor = db.rawQuery(selectQuery, null);  // cr
        // we can write here as db.close() (if required). Manipulating data with cursor is very effiient. 

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ModelOne contact = new ModelOne();
                contact.setVegtharakari(cursor.getString(0));
                contact.setTharakariprice(cursor.getString(1));
                contact.setTharakariweight(cursor.getString(2));
                contact.setTharakariplace(cursor.getString(3));

                // Adding contact to list
                contactListveg.add(contact);
            } while (cursor.moveToNext());             // do this untill the while condition is satisfied. keep repeating the task
            //
        }

        // return contact list
        return contactListveg;
    }

    public List<ModelTwo> getAllContactsTwo() {
        List<ModelTwo> contactListfruits = new ArrayList<ModelTwo>();  // Array list created to hold the data from database ( rows).
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_FRUITS;

        SQLiteDatabase db = this.getWritableDatabase(); //this is requesting database to allow to write some data to it.
        Cursor cursor = db.rawQuery(selectQuery, null);  // cr
        // we can write here as db.close() (if required). Manipulating data with cursor is very effiient. 

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) 
            do {
                ModelTwo contact = new ModelTwo();
                contact.setFruitItems(cursor.getString(0));
                contact.setPrice(cursor.getString(1));
                contact.setWeight(cursor.getString(2));
                contact.setPlace(cursor.getString(3));

                // Adding contact to list
                contactListfruits.add(contact);
            } while (cursor.moveToNext());             // do this untill the while condition is satisfied. keep repeating the task
        //


        // return contact list
        return contactListfruits;
    }


}

3 个答案:

答案 0 :(得分:0)

问题出在这条线上。

CREATE TABLE Vegetable(idt INTEGER PRIMARY KEY,vegtharakari TEXT,tharakariprice TEXT,tharakariweight TEXT,tharakariplace TEXT,)

创建表格查询在 tharakariplace TEXT 中有额外的comma。删除它并尝试

答案 1 :(得分:0)

删除Create Table查询末尾的额外逗号。

private static final String CREATE_TABLE_VEGETABLE =  "CREATE TABLE " 
        + TABLE_VEGETABLE
        + "("
        + KEY_ID 
        + " INTEGER PRIMARY KEY," 
        + KEY_VEGITEMS 
        + " TEXT,"
        + KEY_PRICE 
        + " TEXT,"
        + KEY_WEIGHT
        + " TEXT," 
        + KEY_PLACE
        + " TEXT," 
        + " )";

TEXT,

之后将TEXT更改为KEY_PLACE

答案 2 :(得分:0)

读取此android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE Vegetable(idt INTEGER PRIMARY KEY,vegtharakari TEXT,tharakariprice TEXT,tharakariweight TEXT,tharakariplace TEXT,)

您的创建表查询错误。最后添加一个额外的昏迷(,)。

按如下方式更改您的查询。

 private static final String CREATE_TABLE_VEGETABLE =  "CREATE TABLE " 
            + TABLE_VEGETABLE
            + "("
            + KEY_ID 
            + " INTEGER PRIMARY KEY," 
            + KEY_VEGITEMS 
            + " TEXT,"
            + KEY_PRICE 
            + " TEXT,"
            + KEY_WEIGHT
            + " TEXT," 
            + KEY_PLACE
            + " TEXT" 
            + " )";