Helloworld database - android connection with sqlite?

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.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

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 .." + "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 java android database sqlite hello-world link|improve this question asked Sep 20 '11 at 17:36omniyo153 50% accept rate.

– roman Sep 20 '11 at 17:41 The application HelloTestSQL (process com. SQLearning) has stopped unexpectedly. Please try again.

– omniyo Sep 20 '11 at 17:45 use the debugger (i think run->debug in eclipse) - then you can get a stacktrace ... – roman Sep 20 '11 at 17:46.

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

Thanks daniel novak and roman – omniyo Sep 21 '11 at 12:22 You can mark my message as "Answer" if it helped you and the problem is resolved :-). – Daniel Novak Sep 21 '11 at 12:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions