开发者

Helloworld database - android connection with sqlite

开发者 https://www.devze.com 2023-04-06 08:40 出处:网络
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.

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

        int开发者_如何学C 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 2 has "+seats);
    }
}

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

    static final String viewTables="ViewTables";

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

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

      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.


I think Adil want to say this things..

----------


/**open the db**/   


----------


 DbHelper = new DatabaseHelper(context);

db = mDbHelper.getWritableDatabase();

/**Closes the OLQ Database*/
public void closeOLQ() 
{
    mDbHelper.close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消