i still have problem with this sqlitedatabase. you know i want to store and retrieve data in sqlite database. i have a table named tour with five column. my database has been create and i could see it via filemanager but the problem is that the both insert and read method doesn't work. i find it out by getting the count of the row by this command i have a class that manage the database named: SQLopenHelper with two method for add and read
and call the method in MainActivity
cursor.getCount();
it's give me 0
my SQLopenHelper class:
package com.google.site.sqlHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Sql_openHelper extends SQLiteOpenHelper {
public Sql_openHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String DATABASE_NAME="tours.db";
private static final int DATABASE_VERSION=1;
private static final String TABLE_TOURS="tours";
private static final String COLUMN_ID="tourId";
private static final String COLUMN_TITLE="title";
private static final String COLUMN_DESC="description";
private static final String COLUMN_PRICE="price";
private static final String COLUMN_IMAGE="image";
private static final String TABLE_CREATE="CREATE TABLE "+TABLE_TOURS + " ("+
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_DESC + " TEXT, "+
COLUMN_IMAGE+" TEXT, " +
COLUMN_PRICE + " TEXT " +
" )";
private static final String SQL_DELETE_ENTRIES=
"DROP TABLE IF EXISTS " + TABLE_TOURS;
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
db.close();
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onUpgrade(db,oldVersion,newVersion);
}
public void InsertDb(tour tour)
{
ContentValues values=new ContentValues();
values.put(COLUMN_TITLE, tour.getTitle());
values.put(COLUMN_DESC, tour.getDescription());
values.put(COLUMN_PRICE, tour.getPrice());
values.put(COLUMN_IMAGE, tour.getImage());
SQLiteDatabase db=this.getWritableDatabase();
db.insert(TABLE_TOURS, null, values);
Log.d("InsertDb method", "InsertDb method operation has been done");
}
public tour Readdb(tour tour)
{
long count=0;
SQLiteDatabase db=this.getReadableDatabase();
String[] tableColumns = new String[] {
COLUMN_ID,COLUMN_TITLE,COLUMN_DESC,COLUMN_PRICE,COLUMN_IMAGE
};
try{
Cursor cursor =
db.query(TABLE_TOURS, // a. table
tableColumns, // b. column names
COLUMN_TITLE+ " like '"+tour.getTitle()+"'", // c. selections
new String[] { COLUMN_TITLE }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
cursor.moveToFirst();
count=cursor.getCount();
tour.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)));
} catch(Exception e){Log.d("Error: ",e.getMessage() );}
Log.d("Read method", "Read method operation has been done "+String.valueOf(count));
return tour;
}
}
and this is my MainActivity class:
package com.google.site.android_developer_sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import com.google.site.sqlHelper.Sql_openHelper;
import com.google.site.sqlHelper.UIHelper;
import com.google.site.sqlHelper.tour;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Sql_openHelper helper=new Sql_openHelper(this);
tour tour=new tour();
tour.setTitle("Happy birthday");
tour.setDescription("you are the best one that i love in this world");
tour.setImage("you don't need image of u're self");
tour.setPrice("25$");
tour.setTours("no need");
helper.InsertDb(tour);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void btnclick(View view)
{
Sql_openHelper helper=new Sql_openHelper(this);
tour tour=new tour();
tour=helper.Readdb(tour);
UIHelper.displayText(this, R.id.textview, tour.getTitle() + " " + tour.getDescription());
}
the main problem is that the cursor.getCount(); return 0 so it's means my method insert method doesn't work but it don't know why it doesn't through an error!!!
Rewrite the btnclick just like this
public void btnclick(View view)
{
Sql_openHelper helper=new Sql_openHelper(this);
tour tour=new tour();
tour.setTitle("Happy");
tour=helper.Readdb(tour);
UIHelper.displayText(this, R.id.textview, tour.getTitle() + " " + tour.getDescription());
}