1. Download our Official Android App: Forums for Android!

Apps SQLite issues. Please help!!

Discussion in 'Android Development' started by Gimbal5401, Aug 5, 2011.

  1. Gimbal5401

    Gimbal5401 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jul 16, 2010

    Jul 16, 2010
    3
    0
    5
    I'm new to this android programming lark (although not to programming) and am trying to build a simple application that needs to store some information to a SQLite database.
    I have implemented a DatabaseHelper.java class and created an instance of it in my activity, but the code falls into the catch block when i try to do an insert.

    I have a feeling that its something simple like I haven't actually created the database properly, but I cant spot where the problem lies.

    Could someone help me please?
    Even a nod in the right direction would be greatly appreciated.

    The DatabaseHelper.java and the Avtivity I am calling it from are shown below.

    Thanks.

    DatabaseHelper.java

    package com.kingshaybulling.www;

    import android.app.AlertDialog;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;


    public class DatabaseHelper extends SQLiteOpenHelper {

    static final String dbName="bullingDB";
    static final String cowTable="Cow";
    static final String colID="_id";
    static final String colNumber="CowNumber";
    static final String colDate="CowDate";
    static final String colObserved="CowObserved";

    static final String viewCows="ViewCows";

    public DatabaseHelper(Context context) {
    super(context, dbName, null,33);

    // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    AlertDialog alertDialog;
    alertDialog = new AlertDialog.Builder(null).create();
    alertDialog.setTitle("HERE");
    alertDialog.setMessage("HERE!!!");
    alertDialog.show();

    try
    {
    db.execSQL("CREATE TABLE "+cowTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
    colNumber+" TEXT, "+colDate+" Integer, "+colObserved+" TEXT);");


    db.execSQL("CREATE VIEW "+viewCows+
    " AS SELECT "+cowTable+"."+colID+" AS _id,"+
    " "+cowTable+"."+colNumber+","+
    " "+cowTable+"."+colDate+","+
    " "+cowTable+"."+colObserved+""+
    " FROM "+cowTable );

    }
    catch(Exception Ex)
    {

    }
    finally
    {

    }
    }

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

    void AddCow(Cow cow)
    {
    SQLiteDatabase db= this.getWritableDatabase();

    ContentValues cv=new ContentValues();

    cv.put(colNumber, cow.getNumber());
    cv.put(colDate, cow.getCowDate());
    cv.put(colObserved, cow.getObserved());
    //cv.put(colDept,2);

    db.insert(cowTable, colNumber, cv);
    db.close();
    }

    int getCowCount()
    {
    SQLiteDatabase db=this.getWritableDatabase();
    Cursor cur= db.rawQuery("Select * from "+cowTable, null);
    int x= cur.getCount();
    cur.close();
    return x;
    }

    Cursor getAllCows()
    {
    SQLiteDatabase db=this.getWritableDatabase();

    //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
    Cursor cur= db.rawQuery("SELECT * FROM "+viewCows,null);
    return cur;
    }
    }





    Activity.java

    package com.kingshaybulling.www;

    import java.math.BigDecimal;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.TextView;

    public class AddCowActivity extends Activity {
    DatabaseHelper dbHelper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addcow);

    //Handle button
    Button SaveButton = (Button) findViewById(R.id.button1);
    SaveButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    HandleSaveButtonClick();
    }
    });
    }

    private void HandleSaveButtonClick() {

    //set up text boxes
    EditText cowText = (EditText) findViewById(R.id.editText1);
    //set up the radio buttons
    RadioButton rb0 = (RadioButton) findViewById(R.id.radio0);
    RadioButton rb1 = (RadioButton) findViewById(R.id.radio1);

    //set up getting values
    String cowString = cowText.getText().toString();
    String Observed = "";
    String Served = "";
    if (rb0.isChecked()==true)
    {
    Observed = "True";
    Served = "False";
    }
    if (rb1.isChecked()==true)
    {
    Observed = "False";
    Served = "True";
    }

    //set the text view to see if working
    TextView result1 = (TextView) findViewById(R.id.textView2);
    TextView result2 = (TextView) findViewById(R.id.textView3);
    TextView result3 = (TextView) findViewById(R.id.textView4);

    result1.setText(cowString);
    result2.setText(Observed);
    result3.setText(Served);

    String currentDateString = DateFormat.getDateInstance().format(new Date());

    try
    {
    Cow cow=new Cow(cowString, currentDateString, Observed);
    result2.setText("done cow");
    dbHelper.AddCow(cow);
    result2.setText("added cow");
    }
    catch(Exception ex)
    {
    //ok=false;
    //CatchError(ex.toString());
    result3.setText("Error");
    }
    finally
    {
    //if(ok)
    //{
    //NotifyEmpAdded();
    //Alerts.ShowEmpAddedAlert(this);
    //txtEmps.setText("Number of employees "+String.valueOf(dbHelper.getEmployeeCount()));
    //}
    }
    }

    public static double round(double unrounded, int precision, int roundingMode){
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
    }
    }
     

    Advertisement

  2. mills2533

    mills2533 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    150
    Joined:
    Dec 21, 2010

    Dec 21, 2010
    150
    22
    38
    Colorado (USA)
    I think your issue is with getting the database. I could never get getWritableDatabase to work.

    So instead of this:
    SQLiteDatabase db= this.getWritableDatabase();

    You may try:
    SQLiteDatabase db= SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    You'll need to define myPath as:
    String myPath = dbPath + dbName;

    I see you have dbName defined but not the actual path.

    dbPath would be like this if you are storing the data on the phone and not on the SIM:
    static final String dbPath="/data/data/com.kingshaybulling.www/databases/";
     
    Gimbal5401 likes this.
  3. Gimbal5401

    Gimbal5401 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jul 16, 2010

    Jul 16, 2010
    3
    0
    5
    Thanks for getting back to me on this.
    I've made the changes that you suggest and am still getting an error.
    Its all good until i call dbHelper.AddCow(cow);
    and i get a null pointer exception.

    I'm not sufficiently used to eclipse and java to be able to trace the problem and work out exactly what the issue is, so I'm really stuck.
    Do you have any ideas on what I can try next?

    thanks

    EDIT:

    I've spotted that i was missing a call (dbHelper = new DatabaseHelper(this);)

    I now have a different error:

    SQLiteCantOpenDatabaseException:unable to open database file

    so would that point to something i've done wrong in the helper class where I've created the database?
     
  4. mills2533

    mills2533 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    150
    Joined:
    Dec 21, 2010

    Dec 21, 2010
    150
    22
    38
    Colorado (USA)
    That is possible. I'd check to make sure the database file exists under the path and filename you are using. You can do that using "adb shell" if you're familiar with that. You can also open the database file using sqlite while in the adb to be certain it can be opened.
     
  5. Gimbal5401

    Gimbal5401 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jul 16, 2010

    Jul 16, 2010
    3
    0
    5
    I dont really understand the adb shell thing.
    Will have to look into it.
    cant believe that this problem has taken up so much of my time. :(
    never mind. at least when i finally figure it out I'll never have the same issue again. lol
     
  6. mills2533

    mills2533 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    150
    Joined:
    Dec 21, 2010

    Dec 21, 2010
    150
    22
    38
    Colorado (USA)
  7. eugene889

    eugene889 Lurker
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jan 12, 2012

    Jan 12, 2012
    3
    0
    5

Share This Page

Loading...