該項目由兩個類組成,一個用於主要活動,另一個用於擴展Sqliteopenhelper類以便在數據庫上執行查詢的數據庫幫助程序類。添加數據按鈕不會將數據插入數據庫時發生問題。可能,「插入數據」方法有問題,我無法弄清楚。或者是別的什麼?SQLite數據庫沒有插入值
MainActivity.java
public class MainActivity extends Activity {
DatabaseHelper myDb;
EditText editName,editF1,editF2,editF3,editF4,editF5;
Button btnAddData;
Button btnViewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editName = (EditText)findViewById(R.id.editText_name);
editF1 = (EditText)findViewById(R.id.editText_F1);
editF2 = (EditText)findViewById(R.id.editText_F2);
editF3= (EditText)findViewById(R.id.editText_F3);
editF4= (EditText)findViewById(R.id.editText_F4);
editF5= (EditText)findViewById(R.id.editText_F5);
btnAddData = (Button)findViewById(R.id.button_add);
btnViewAll = (Button)findViewById(R.id.button_viewAll);
AddData();
viewAll();
}
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted =
myDb.insertData(editName.getText().toString(), editF1.getText().toString(),
editF2.getText().toString(), editF3.getText().toString(),
editF4.getText().toString(), editF5.getText().toString());
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data
Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not
Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
public void viewAll() {
btnViewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Name :"+ res.getString(0)+"\n");
buffer.append("Teaching Skills :"+
res.getString(1)+"\n");
buffer.append("Competency :"+
res.getString(2)+"\n");
buffer.append("Approachability :"+
res.getString(3)+"\n");
buffer.append("Class Control :"+
res.getString(4)+"\n");
buffer.append("Punctuality :"+
res.getString(5)+"\n\n");
}
// Show all data
showMessage("Data",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
DatabaseHelper.java
package com.dot.myapplication;
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 {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_2 = "NAME";
public static final String COL_3 = "F1";
public static final String COL_4 = "F2";
public static final String COL_5 = "F3";
public static final String COL_6 = "F4";
public static final String COL_7 = "F5";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (NAME TEXT,F1 TEXT,F2 TEXT,F3
TEXT,F4 TEXT,F5 TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String F1s,String F2s,String
F3s,String F4s,String F5s) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,F1s);
contentValues.put(COL_4,F2s);
contentValues.put(COL_5,F3s);
contentValues.put(COL_6,F4s);
contentValues.put(COL_7,F5s);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
每當添加數據按鈕時沒有插入敬酒被調用數據。作爲初學者,我無法弄清楚哪裏出了問題。這是insertData方法的東西嗎?請建議更改。
用'insertOrThrow'替換'insert'。 –