为什么数据没有插入SQLiteDatabase中

时间:2017-10-18 07:29:06

标签: java android sqlite sqliteopenhelper

我在MainActvity上有4个按钮。当我单击Add Button时,将打开AddData类。在这里,我必须插入姓名,电话号码和电子邮件。当我点击进入 保存AddData类的按钮,然后Toast显示消息"未插入数据。"

对不起我的语法。

MainActivity.java

package csitmnr.sqlitedatabaseexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
  Button search,add,delete,update;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    search = (Button) findViewById(R.id.btnSearch);
    add = (Button) findViewById(R.id.btnAdd);
    delete = (Button) findViewById(R.id.btnDelete);
    update = (Button) findViewById(R.id.btnUpdate);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this,AddData.class);
            startActivity(intent);
        }
    });
}
}

DBHelper.java

package csitmnr.sqlitedatabaseexample;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class DBHelper extends SQLiteOpenHelper {

   private static final String DATABASE_NAME = "contacts.db";
   private static final String TABLE_NAME = "contacts";

   public DBHelper(Context context) {
     super(context, DATABASE_NAME, null, 1);


   }

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE "+TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PHONE_NO TEXT,EMAIL TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXIST "+TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public boolean insertData(String name,String phone_no,String email){

    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();

    contentValues.put("NAME",name);
    contentValues.put("PHONE_NO",phone_no);
    contentValues.put("EMAIL",email);

    long result = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);

    if (result == -1)
        return false;
    else
        return true;


}
}

AddData.java

package csitmnr.sqlitedatabaseexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddData extends AppCompatActivity {
  EditText name , phone, email;
  Button save;
  private DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_data);

    dbHelper = new DBHelper(this);

    name = (EditText) findViewById(R.id.etName);
    phone = (EditText) findViewById(R.id.etPhone);
    email = (EditText) findViewById(R.id.etEmail);
    save = (Button) findViewById(R.id.saveButton);


    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean result = dbHelper.insertData(name.getText().toString(),phone.getText().toString(),email.getText().toString());
                if (result){
                    Toast.makeText(AddData.this, "Data is inserted", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(AddData.this, "Data is not inserted", Toast.LENGTH_SHORT).show();
                }
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

来自评论:

  

由于您的应用已经创建了一个表,请尝试   首先清除数据并再次运行您的应用程序。 - Jerrol 1分钟前

简单的解决方案,因为您在升级时处理旧表,只需尝试增加表版本。

问题的原因是您在不增加版本号的情况下更改了数据库的格式,因此可能的解决方案是:

  • 增加版本号并正确实施onUpgrade()
  • 重新安装应用程序(假设非持久存储)
  • 清除应用数据