我有一个与sqlite相关的项目。当我添加数据时,应用程序强制关闭。我已经能够通过代码向sqlite添加数据,但是在通过表单添加数据时遇到了困难。我经历了app力量关闭,有什么人可以帮助我?
这是我的代码
PemainDbAdapter.java
public class PemainDbAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_NIM = "nim";
public static final String KEY_NAMA = "nama";
public static final String KEY_JURUSAN = "jurusan";
public static final String KEY_TELP = "no_telp";
public static final String KEY_UMUR = "umur";
public static final String TAG = "CountriesDbAdapter";
public DatabaseHelper mDbHelper;
public SQLiteDatabase mDb;
public static final String DATABASE_NAME = "mvp";
public static final String SQLITE_TABLE = "daftar_pemain";
public static final int DATABASE_VERSION = 1;
public final Context mCtx;
public static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_NIM + "," +
KEY_NAMA + "," +
KEY_JURUSAN + "," +
KEY_TELP + "," +
KEY_UMUR + "," +
" UNIQUE (" + KEY_NIM +"));";
public static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public PemainDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public PemainDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createPemain(String nim, String nama, String jurusan, String no_telp, String umur) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NIM, nim);
initialValues.put(KEY_NAMA, nama);
initialValues.put(KEY_JURUSAN, jurusan);
initialValues.put(KEY_TELP, no_telp);
initialValues.put(KEY_UMUR, umur);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllPemain() {
int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchPemainByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_NIM, KEY_NAMA, KEY_JURUSAN, KEY_TELP, KEY_UMUR},
null, null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_NIM, KEY_NAMA, KEY_JURUSAN, KEY_TELP, KEY_UMUR},
KEY_NAMA + " like '%" + inputText + "%'", null,
null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllPemain() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_NIM, KEY_NAMA, KEY_JURUSAN, KEY_TELP, KEY_UMUR},
null, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void insertSomePemain() {
createPemain("125410011", "Adi Saputra", "Teknik Informstika", "081904445501", "20 Tahun");
createPemain("125410012", "Mukti Setyawan", "Teknik Informatika", "081904445502", "19 Tahun");
createPemain("125410013", "Titit", "Siatem Informasi", "081904445503", "21 Tahun");
}
Input_Pemain.java
public class Input_Pemain extends Activity {
public PemainDbAdapter dbHelper;
private EditText editTextNim, editTextNama, editTextJurusan, editTextTelepon, editTextUmur;
private Button btnInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input__pemain);
this.editTextNim = (EditText) this.findViewById(R.id.editNIM);
this.editTextNama = (EditText) this.findViewById(R.id.editNama);
this.editTextJurusan = (EditText) this.findViewById(R.id.editJurusan);
this.editTextTelepon = (EditText) this.findViewById(R.id.editTelepon);
this.editTextUmur = (EditText) this.findViewById(R.id.editUmur);
this.btnInput = (Button) this.findViewById(R.id.btnInput);
btnInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nim = editTextNim.getText().toString().trim();
String nama = editTextNama.getText().toString().trim();
String jurusan = editTextJurusan.getText().toString().trim();
String no_telp = editTextTelepon.getText().toString().trim();
String umur = editTextUmur.getText().toString().trim();
if(nim.equals("")){
Toast.makeText(getApplicationContext(), "Tolong masukan data lengkap!", Toast.LENGTH_SHORT).show();
}else if(nama.equals("")){
Toast.makeText(getApplicationContext(), "Tolong masukan data lengkap!", Toast.LENGTH_SHORT).show();
}else if(jurusan.equals("")){
Toast.makeText(getApplicationContext(), "Tolong masukan data lengkap!", Toast.LENGTH_SHORT).show();
}else if(no_telp.equals("")){
Toast.makeText(getApplicationContext(), "Tolong masukan data lengkap!", Toast.LENGTH_SHORT).show();
}else if(umur.equals("")){
Toast.makeText(getApplicationContext(), "Tolong masukan data lengkap!", Toast.LENGTH_SHORT).show();
}else {
dbHelper = new PemainDbAdapter(Input_Pemain.this);
dbHelper.createPemain(nim, nama, jurusan, no_telp, umur);
}
}
});
}
请帮帮我!
答案 0 :(得分:1)
您只在createPemain()中传递第一个参数null。
在插入查询中找不到RId字段。 试试吧......
这对你有帮助。