Android的 - SQLite数据库的方法未定义FOT类型 [英] Android - Sqlite database method undefined fot type
问题描述
我的类在这个应用程序,应该创建并使用一个数据库,但是Eclipse告诉我,sqlite的方法是不确定的。
i've a class in this app that should create and use a database, but Eclipse tell me that the sqlite methods are undefined.
似乎是一个环境问题,但我不知道如何解决,有我延长活动的不同类的呢?
Seem to be a context problem but i don't understand how to fix, have i to extend a different class instead of Activity?
package com.android.userdata;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class getdata extends Activity {
private final String MY_DATABASE_NAME = "DataStore";
private final String MY_DATABASE_TABLE = "UserData";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdata);
SQLiteDatabase myDB = null;
/* Create the Database (no Errors if it already exists) */
this.createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
myDB = this.openDatabase(MY_DATABASE_NAME, null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (LastName VARCHAR, FirstName VARCHAR,"
+ " Country VARCHAR, Age INT(3));");
Button btn = (Button) findViewById(R.id.confirm);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getdata.this, "Hai Premuto il Pulsante", Toast.LENGTH_SHORT).show();
}
});
}
}
感谢您的帮助!
Thanks for your help!
推荐答案
我建议你使用SQLiteOpenHelper。
I suggest you use the SQLiteOpenHelper.
它会自动关心创建一个数据库,如果它是不可用......
It cares automatically to create a DB if it's not available...
例如code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "myDB.db";
public DatabaseHelper(Context ctx) {
super(ctx, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE myTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL)";
db.execSQL(query);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
...
}
}
使用方法如下:
...
DatabaseHelper helper = new DatabaseHelper(context);
SQLiteDatabase db = helper.getWritableDatabase();
...
我认为这是更简单,你不需要关心,如果数据库是存在的了。
I think it's easier and you don't need to care about if the DB is existing already.
希望这有助于:)
编辑:
我有一个想法......也许这是不可能把这个字符串像你这样做...尝试之前建立的字符串。 像这样的:
I have an idea... maybe it is not possible to give this string like you are doing... try to build the string before. Like this:
String query = "YOUR SQL STATEMENT";
db.execSQL(query);
db.close();
这样的可能性:)
Is a possibility :)
这篇关于Android的 - SQLite数据库的方法未定义FOT类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!