1

I am inserting the data from data base using sqlite but I am getting an error .Here is my code and error.I just enter one entry and want to display that entry ?

package com.example.database_example;

public class Information {

    String name;
    public Information(String name) {
        // TODO Auto-generated constructor stub
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Data Base.java class

package com.example.database_example;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Information inf=new Information("naveen");
        DataBaseExample dbx=new DataBaseExample(MainActivity.this);

        if(dbx.insertname(inf)){
            Log.v("checkdbx.insertname(inf);", "save ok.");
        }else{
            Log.v("checkdbx.insertname(inf);", "save failed.");
        }

    }



}
package com.example.database_example;

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

public class DataBaseExample extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "information";

    // Contacts table name
    private static final String TABLE_Name= "Name";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";

    public DataBaseExample(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
        String createTable= "CREATE TABLE " + TABLE_Name+"("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT,"
                + ")";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);

            // Create tables again
            onCreate(db);
    }

    public boolean insertname(Information information) {

        boolean createSuccessful = false;

        ContentValues values = new ContentValues();

      //  values.put(KEY_ID, information.getId());
        values.put(KEY_NAME, information.getName());

        SQLiteDatabase db = this.getWritableDatabase();

        createSuccessful = db.insert(TABLE_Name, null, values) > 0;
        db.close();

        return createSuccessful;
    }

}

Here is my error

09-24 07:25:07.138: I/Database(392): sqlite returned: error code = 1, msg = near ")": syntax error
09-24 07:25:07.138: E/Database(392): Failure 1 (near ")": syntax error) on 0x2a7080 when preparing 'CREATE TABLE Name(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,)'.
09-24 07:25:07.148: D/AndroidRuntime(392): Shutting down VM
09-24 07:25:07.148: W/dalvikvm(392): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-24 07:25:07.158: E/AndroidRuntime(392): FATAL EXCEPTION: main
09-24 07:25:07.158: E/AndroidRuntime(392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database_example/com.example.database_example.MainActivity}: android.database.sqlite.SQLiteException: near ")": syntax error: CREATE TABLE Name(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.os.Looper.loop(Looper.java:123)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-24 07:25:07.158: E/AndroidRuntime(392):  at java.lang.reflect.Method.invokeNative(Native Method)
09-24 07:25:07.158: E/AndroidRuntime(392):  at java.lang.reflect.Method.invoke(Method.java:507)
09-24 07:25:07.158: E/AndroidRuntime(392):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-24 07:25:07.158: E/AndroidRuntime(392):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-24 07:25:07.158: E/AndroidRuntime(392):  at dalvik.system.NativeStart.main(Native Method)
09-24 07:25:07.158: E/AndroidRuntime(392): Caused by: android.database.sqlite.SQLiteException: near ")": syntax error: CREATE TABLE Name(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
09-24 07:25:07.158: E/AndroidRuntime(392):  at com.example.database_example.DataBaseExample.onCreate(DataBaseExample.java:34)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
09-24 07:25:07.158: E/AndroidRuntime(392):  at com.example.database_example.DataBaseExample.insertname(DataBaseExample.java:55)
09-24 07:25:07.158: E/AndroidRuntime(392):  at com.example.database_example.MainActivity.onCreate(MainActivity.java:18)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-24 07:25:07.158: E/AndroidRuntime(392):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-24 07:25:07.158: E/AndroidRuntime(392):  ... 11 more
5
  • Try removing last , here ,name TEXT,)? Commented Sep 24, 2013 at 2:51
  • The comma after name Text: ? 'CREATE TABLE Name(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,) Commented Sep 24, 2013 at 2:52
  • I did not write antthing like this String createTable= "CREATE TABLE " + TABLE_Name+"(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + ")"; Commented Sep 24, 2013 at 2:55
  • Got it ..How to read this value ? Commented Sep 24, 2013 at 2:57
  • I am able to save that value but Now I want to read that value how to read that value Commented Sep 24, 2013 at 2:58

1 Answer 1

0

Change

String createTable= "CREATE TABLE " + TABLE_Name+"("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT,"
            + ")";

to

String createTable= "CREATE TABLE " + TABLE_Name+"("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT"
            + ")";

removing the comma after TEXT

EDIT

To read all the values from the database, you can add the following method to your class DataBaseExample:

private List<Information> getAllItems()
    List<Information> itemsList = new ArrayList<Information>();
    Cursor cursor = null;
    try {
        //get all rows
        cursor = mDatabase.query(TABLE_Name, null, null, null, null,
                null, null);
        if (cursor.moveToFirst()) {
            do {
                Information c = new Information();
                c.setName(cursor.getString(KEY_NAME));
                itemsList.add(c);
            } while (cursor.moveToNext());
        }
    } catch (SQLiteException e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }
    return itemsList;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Do you mean you want to read the value from KEY_NAME column based on id or something?
Actually I set the name value "naveen" , see my code .If I save another value like "Ravi".Now I want to print all value from Table Mean "naveen and ravi"
This will be something similar to the code in this answer: stackoverflow.com/questions/6847259/…
Yes I understand but can you do some favour for me can you change in my code because it is too difficult to understand the code for beginner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.