我是android编程的新手。我正在尝试创建一个数据库 一张桌子,但它崩溃了:
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db";
public static final String CREATE_ITEMS_TABLE = "CREATE TABLE items ( _ID INTEGER PRIMARY KEY, code INTEGER," + "item TEXT, description TEXT);";
private static final String put = "insert into items (code,item,description) values (1,obj,obj info)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_ITEMS_TABLE);
db.execSQL(put);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("Constants", "Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS constants");
onCreate(db);
}
}
Cursor c;
DatabaseHelper db = new DatabaseHelper(this);
c = db.getReadableDatabase().rawQuery("SELECT * FROM items ORDER BY code;", null);
if(c.moveToFirst()){
String code= c.getString(1);
String item= c.getString(2);
String info= c.getString(3);
}
结果我只得到第一个字段,第二个是字段名称,第三个使程序崩溃。
答案 0 :(得分:0)
所以,这里有一个建议,应该为你做得更好。我刚刚进行了测试,看它是否有效,并且在没有崩溃的情况下执行,并提供一点输出来验证数据。
我将它分成两个文件,这只是个人风格。
首先,您的DatabaseHelper类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by shellstrom on 2017-05-29.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db";
public static final String CREATE_ITEMS_TABLE = "CREATE TABLE items (_ID INTEGER PRIMARY KEY, code INTEGER, item TEXT, description TEXT)";
private static final String put = "INSERT INTO items (code,item,description) values ('','1', 'obj', 'obj info')";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_ITEMS_TABLE);
db.execSQL(put);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("Constants", "Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS items");
onCreate(db);
}
}
然后我从MainActivity类运行程序:
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cursor c;
DatabaseHelper db = new DatabaseHelper(this);
c = db.getReadableDatabase().rawQuery("SELECT * FROM items ORDER BY code", null);
Log.d("TAG", "c count" +c.getCount());
if(c.moveToFirst()){
String code= c.getString(1);
String item= c.getString(2);
String info= c.getString(3);
Log.d("TAG", "c1: " +code+ " c2: " +item+ " c3: " +info);
}
}
}
现在,我没有花时间修复其他问题,比如进行参数化查询,错误处理等等。我只想给你至少一些根据你的样本编译和运行的东西。如果您有任何疑问,请随时提出。