user3257386
user3257386

Reputation: 284

why I cant read form my database?

I can insert informations in my database but when I want to read them after insert it like developer.android it will be close with this error "unfortunately,app has stopped".
this is my code:

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getApplicationContext());
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues(); //for insert
Date now=new Date(); //for insert
values.put("ID", 1); //for insert
values.put("kalame", "good"); //for insert
values.put("mani", "salam"); //for insert
values.put("marhale", "1"); //for insert
values.put("taghir", String.valueOf(now.getTime())); //for insert

long newRowID=db.insert("english", "taghir" , values); //for insert

String[] meghdarha={"good"};
String[] sotoonha={"ID","kalame","mani","marhale","taghir"};
Cursor nowRead =db.query("english", sotoonha, "kalame = good", null, null, null, null);

nowRead.moveToFirst();
long newWord=nowRead.getLong(nowRead.getColumnIndexOrThrow("ID"));
TextView tv=    (TextView)findViewById(R.id.textView1);
tv.setText(String.valueOf(newWord));  

this is my FeedReaderDbHelper:

public class FeedReaderDbHelper extends SQLiteOpenHelper {

    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";
    public static final String sakht="CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT );";
    public static final String pak="DROP TABLE IF EXISTS english";

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

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

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(pak);
        onCreate(db);
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }    
}

this is my log cat:

02-09 18:58:22.762: D/gralloc_goldfish(581): Emulator without GPU emulation detected.
02-09 18:59:51.202: E/SQLiteDatabase(581): Error inserting ID=1 mani=salam taghir=1391959791144 marhale=1 kalame=good
02-09 18:59:51.202: E/SQLiteDatabase(581): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at ir.maghsoodi.leitnerbox.Insert.onCreate(Insert.java:31)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.Activity.performCreate(Activity.java:4465)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.os.Looper.loop(Looper.java:137)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at java.lang.reflect.Method.invokeNative(Native Method)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at java.lang.reflect.Method.invoke(Method.java:511)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 18:59:51.202: E/SQLiteDatabase(581):  at dalvik.system.NativeStart.main(Native Method)
02-09 18:59:51.232: D/AndroidRuntime(581): Shutting down VM
02-09 18:59:51.242: W/dalvikvm(581): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
02-09 18:59:51.342: E/AndroidRuntime(581): FATAL EXCEPTION: main
02-09 18:59:51.342: E/AndroidRuntime(581): java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.maghsoodi.leitnerbox/ir.maghsoodi.leitnerbox.Insert}: android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException: bind or column index out of range: handle 0x1ee560
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.os.Looper.loop(Looper.java:137)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 18:59:51.342: E/AndroidRuntime(581):  at java.lang.reflect.Method.invokeNative(Native Method)
02-09 18:59:51.342: E/AndroidRuntime(581):  at java.lang.reflect.Method.invoke(Method.java:511)
02-09 18:59:51.342: E/AndroidRuntime(581):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 18:59:51.342: E/AndroidRuntime(581):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 18:59:51.342: E/AndroidRuntime(581):  at dalvik.system.NativeStart.main(Native Method)
02-09 18:59:51.342: E/AndroidRuntime(581): Caused by: android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException: bind or column index out of range: handle 0x1ee560
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:247)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:299)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:401)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:55)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
02-09 18:59:51.342: E/AndroidRuntime(581):  at ir.maghsoodi.leitnerbox.Insert.onCreate(Insert.java:38)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.Activity.performCreate(Activity.java:4465)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 18:59:51.342: E/AndroidRuntime(581):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 18:59:51.342: E/AndroidRuntime(581):  ... 11 more
02-09 19:04:51.563: I/Process(581): Sending signal. PID: 581 SIG: 9

how can I fix it?
help me please

Upvotes: 0

Views: 137

Answers (2)

Jim
Jim

Reputation: 10288

"constraint failed" has to do with your primary key. You are trying to insert a row with a duplicate "ID"

When you create your table:

CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT );"

you are telling SQLite that the column "ID" cannot have duplicate values. When you restart your app, your database already exists and you are trying to insert a row with "ID=1" again here:

long newRowID=db.insert("english", "taghir" , values); //for insert

Upvotes: 0

Jacky So
Jacky So

Reputation: 233

Not sure if this is the problem but you create Table query is missing the ");"

you have

public static final String sakht="CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT";

but it should be

public static final String sakht="CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT);";

Also your query is wrong. You can either do

Cursor nowRead =db.query("english", sotoonha, "kalame = good", null, null, null, null);

or to be more dynamic

Cursor nowRead =db.query("english", sotoonha, "kalame = ?", meghdarha, null, null, null);

check out the following post for details How to pass two or more selection argument in "query" method

Upvotes: 1

Related Questions