java - Helloworld 数据库 - Android 与 sqlite 的连接

标签 java android database sqlite

我是 Android 新手。我只需要编写一个简单的 hello-world 应用程序,该应用程序在屏幕上显示有关该应用程序所连接的数据库的一些数据。 这是我的尝试,但它在“选择或显示”部分崩溃。 它由两个文件组成:Main 和 DataBaseHelper。

主要:

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

数据库助手:

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

  }

}

提前致谢。

最佳答案

我想阿迪尔想说这些..

----------


/**open the db**/   


----------


 DbHelper = new DatabaseHelper(context);

db = mDbHelper.getWritableDatabase();

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

关于java - Helloworld 数据库 - Android 与 sqlite 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470393/

相关文章:

Java 泛型 : Special usage <T extends Object & Interface>

java - Solr找不到schema.xml

android - Flutter:将 Proguard 与 Gradle 7.0.4 结合使用

java - Android 上的异步套接字实现

java - j2me数据库查询包括条件

java - 在 hibernate 中选择

android - 发送推送通知时,firebase 是否自行管理设备 token 、bundle id 等用户数据,还是我们必须管理这些数据?

c++ - 如何在 Microsoft Visual C++ 2010 Express 项目中创建本地数据库?

java - SQLite插入函数/方法参数参数太多,如何重构?

mysql - MySQL 在做什么??启动时磁盘利用率为 100%