简体   繁体   中英

Helloworld database - android connection with sqlite

I'm new developing in Android. I need to program just a easy hello-world application that shows in the screen some data about the database to whom the application is connected. Here is my try, but it crashes in the "select or show" part. It is composed of two files: Main and DataBaseHelper.

Main:

package com.SQLearning;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

public class HelloTestSQLActivity extends Activity {
    DatabaseHelper dbHelper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        dbHelper = new DatabaseHelper(this);
        int seats = dbHelper.getSeats("2");
        TextView tv = new TextView(this);
        setContentView(tv);
        tv.setText( "My first Android App: This is a Hello World Test..." +
                    "Database connection in progress..." +
                    "Table 1 has "+seats);

        dbHelper.close();
    }
}

DatabaseHelper:

package com.SQLearning;

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="demoDB";
    static final String tablesTable="Tables";
    static final String colNum="TableNum";
    static final String colSeats="NumSeats";
    private final Context myContext; 

    static final String viewTables="ViewTables";

    public DatabaseHelper(Context context) {
        super(context, dbName, null,2); 
        this.myContext = context;
    }

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

        db = getWritableDatabase();

        db.execSQL("CREATE TABLE "+tablesTable+" ("+colNum+ " INTEGER PRIMARY KEY , "+
                                                  colSeats+ " INTEGER)");


        db.execSQL("CREATE VIEW "+viewTables+
                   " AS SELECT "+tablesTable+"."+colNum+" AS _id,"+
                   " "+tablesTable+"."+colSeats+","+
                   " FROM "+tablesTable);

        //Inserts pre-defined departments
        insertTableRecords();
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub

          db.execSQL("DROP TABLE IF EXISTS "+tablesTable);
          db.execSQL("DROP VIEW IF EXISTS "+viewTables);
          onCreate(db);
     }

    public void insertTableRecords() {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();

        cv.put(colNum, 1);
        cv.put(colSeats, 3);
        db.insert(tablesTable, colNum, cv);

        cv.put(colNum, 2);
        cv.put(colSeats, 6);
        db.insert(tablesTable, colNum, cv);

        cv.put(colNum, 3);
        cv.put(colSeats, 5);
        db.insert(tablesTable, colNum, cv);

        cv.put(colNum, 4);
        cv.put(colSeats, 2);
        db.insert(tablesTable, colNum, cv);

        db.close();
    }

    public int getSeats(String tablenum)
    {
        SQLiteDatabase db=this.getReadableDatabase();

        String[] args = new String[] {"1"};
        Cursor cur = db.rawQuery(" SELECT "+colSeats+" FROM "+tablesTable+" WHERE "+colNum+"=? ", args);

        cur.moveToFirst();
        db.close();
        //return cur.getInt(cur.getColumnIndex(colSeats));
        return cur.getInt(0);

     }
 }

Thanks in advance

You close the database connection and then try to read something from the cursor. Cursor is pointing to your database and it will fail if you close connection in the mean time.

Replace:

   db.close();
   return cur.getInt(0);

With:

  int value= cur.getInt(0);
  db.close();
  return value;

Also always look at the logs (DDMS tab in Eclipse), you can see a description of the exception there.

BTW. You shouldn't use column indexes directly, the code is not very readable. Instead of cur.getInt(0) use cur.getInt(cur.getColumnIndex(colSeats))...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM