simple SQLite code for android

I am trying to write my first SQLite database program with android. a simple program in which with pressing a button I add some data to the data base, and by pressing another button read data from it, and show it with toast. unfortunately the program doesn't work ! I am not sure is it because the data base is not made or the reading process has problem. The program is compiling and SPK file is made, but when I press the button to read from database my tablet will hang and exit the app. the codes are here:

public class MainActivity extends ActionBarActivity {
    Button btn1,btn2 ;
    bookBD db = new bookBD(this);

    protected void onCreate(Bundle savedInstanceState) {
        btn1 = (Button) findViewById(;
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                db.AddBook("one",1,"the first comment");
                db.AddBook("two",2,"the second comment");
        btn2 = (Button) findViewById(;
        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Cursor cr = (new) db.GetBook(1);
                Toast.makeText(MainActivity.this, cr.getString(cr.getColumnIndex(bookname)) , Toast.LENGTH_LONG).show();
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);

and the database code is:

class bookBD extends SQLiteOpenHelper {
    public static final String DB_name = "1sttry.db";
    public static final String Table_Name= "Bookdb";
    public static final String DB_id = "id";
    public static final String DB_bookname = "bookname";
    public static final String DB_price = "price";
    public static final String DB_content = "content";
    private static final String[] COLUMNS = {DB_id,DB_bookname,DB_price,DB_content};
    private static final String[] Columns = {DB_id,DB_bookname,DB_price,DB_content};
    public bookBD(Context context){
        super(context, DB_name, null, 1);

    public void onCreate(SQLiteDatabase db) {
        String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + Table_Name + "("
                + DB_id + " INTEGER PRIMARY KEY," + DB_bookname
                + " bookname," + DB_price + " INTEGER" + DB_content + " CONTENT" + ")";

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

    public void AddBook(String Bname, int Bprice, String Bcontent){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
    public Cursor GetBook(int id){

        SQLiteDatabase db = this.getReadableDatabase();
    Cursor res ; 
        res =  db.rawQuery( "select * from contacts where id="+id+"", null );
    // or 
        //res = db.query (Table_Name,Columns, DB_id + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
        if (res != null)
        return res;

does anybody know why my codes are not working?


You miss a space here:

+ " bookname," + DB_price + " INTEGER" + DB_content + " CONTENT" + ")";  

The table is not created.

Try this:

+ " bookname," + DB_price + " INTEGER " + DB_content + " CONTENT" + ")";

Also here:

db.execSQL("DROP TABLE IF EXISTS books"+Table_Name);

It should be:

db.execSQL("DROP TABLE IF EXISTS books "+Table_Name);


Cursor cr = (new) db.GetBook(1);


Cursor cr = db.GetBook(1);

Also in GetBook() method use the query method you have commented.

Need Your Help

Using dll in unmanaged c++ project

c++ com dll interop

I created a dll called "WSdll.dll".

Using == or .Equals() on nulls and string.Empty

c# null string

In reference to using .Equals() or == on strings, here is a question in regards to checking for string.Empty and null objects.