SQLite数据库没有插入值

时间:2017-09-14 15:59:52

标签: android sqlite android-sqlite

该项目由两个类组成,一个用于主活动,另一个是Database Helper类,它扩展了Sqliteopenhelper类以便在数据库上执行查询。当添加数据按钮未将数据插入数据库时​​,会发生此问题。可能是“插入数据”方法有问题,我无法弄清楚。还是别的什么?

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;
}
}

每当点击添加数据按钮时,都会调用未插入toast的数据。作为初学者,我无法弄清楚出了什么问题。是insertData方法吗?请提出修改建议。

1 个答案:

答案 0 :(得分:1)

您插入是正确的。你必须查询这样的数据。

             if(res.moveToFirst()){
                        do{
                            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");
                        }while (res.moveToNext()) 
             }

数据库帮助程序类中,您在查询数据库后忘记关闭

这是学习SQLite数据库实现的完美教程。 http://www.techotopia.com/index.php/An_Android_SQLite_Database_Tutorial