0

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);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.bbtn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.AddBook("one",1,"the first comment");
                db.AddBook("two",2,"the second comment");
            }
        });
        btn2 = (Button) findViewById(R.id.bbtn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cursor cr = (new) db.GetBook(1);
                Toast.makeText(MainActivity.this, cr.getString(cr.getColumnIndex(bookname)) , Toast.LENGTH_LONG).show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            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);
    }

    @Override
    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" + ")";
        db.execSQL(CREATE_PRODUCTS_TABLE);
    }

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

    public void AddBook(String Bname, int Bprice, String Bcontent){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DB_bookname,Bname);
        values.put(DB_price,Bprice);
        values.put(DB_content,Bcontent);
        db.insert(Table_Name,null,values);
        db.close();
    }
    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)
            res.moveToFirst();
        return res;
    }
}

does anybody know why my codes are not working?

Amir
  • 75
  • 1
  • 7

2 Answers2

0

Change

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

to

Cursor cr = db.GetBook(1);

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

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

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);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115