我是 SQLite 的新手。我正在尝试制作一个使用 SQLite 数据库的简单应用程序。我已经完成了大部分部分,但我遇到了一些运行时异常。
这是我的 DBHelper 的 java 文件-
温度.java:-
package com.example.sqliteexample;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class HotOrNot {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "item_name";
public static final String KEY_TEMPERATURE = "item_temperature";
private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "itemTable";
private static final int DATABASE_VERSION = 1;
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE "+DATABASE_TABLE+" ("+
KEY_ROWID +" INTEGER PRIMARY KEY AUTO INCREMENT, "+
KEY_NAME+" TEXT NOT NULL, "+
KEY_TEMPERATURE+" TEXT NOTNULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
onCreate(db);
}
}
public HotOrNot (Context c){
ourContext = c;
}
public HotOrNot open()throws SQLException{
ourHelper=new DBHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, String hotness) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_TEMPERATURE, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]
{
KEY_ROWID, KEY_NAME,KEY_TEMPERATURE
};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null,null,null,null,null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_TEMPERATURE);
for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result = result + c.getString(iRow)+ " "+
c.getString(iName)+" "+c.getString(iHotness)+"\n";
}
return result;
}
}
我在运行代码时收到 SQLException。代码在语法上很好,但我不明白,哪里出错了。
这是以前发生的事情。但是现在,除此之外,应用程序根本没有启动。logcat 和状态中没有消息 -
'ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.sqliteexample/.SQLiteExample }'
控制台中缺少。任何人都可以给我一个解决方案吗?
提前致谢。