我是android编程的新手。这是我从教程中学到的,但我无法理解为什么应用程序不存储我插入的数据。这是我的代码,但每当我运行应用程序时,它都说没有插入数据。
这是DataBaseHelper类
package com.dutt.rishabh.android.sqlite.contacts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Rishabh on 06-07-2016.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="contacts.db";
public static final String TABLE_NAME="student_contacts";
public static final String id="ID";
public static final String COL_1="NAME";
public static final String COL_2="PHONE NUMBER";
public static final int DATABASE_VERSION=1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query="create table "+TABLE_NAME+"("+id+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_1+" TEXT NOT NULL,"+COL_2+" TEXT NOT NULL"+")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("Drop table if exists "+TABLE_NAME);
onCreate(db);
}
public boolean addData(String name,String num ){
SQLiteDatabase myDb=this.getWritableDatabase();
ContentValues content=new ContentValues();
content.put("NAME",name);
content.put("PHONE NUMBER",num);
long result=myDb.insert(TABLE_NAME,null ,content);
if(result==-1){
return false;
}else{
return true;
}
}
public Cursor viewData(){
SQLiteDatabase myDb=this.getReadableDatabase();
Cursor cr= myDb.rawQuery("Select * from "+DatabaseHelper.TABLE_NAME,null);
return cr;
}
}
**This is MainActivity class**
package com.dutt.rishabh.android.sqlite.contacts;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
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 MainActivity extends AppCompatActivity {
Button add, view;
EditText name, num;
Contact contact;
DatabaseHelper dbHelper= new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.add);
name = (EditText) findViewById(R.id.name);
num = (EditText) findViewById(R.id.num);
view = (Button) findViewById(R.id.view);
//contact = new Contact(name.getText().toString(), num.getText().toString());
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean m = dbHelper.addData(name.getText().toString(),num.getText().toString());
if (m) {
Toast.makeText(MainActivity.this, "Data Inserted!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Data not Inserted!", Toast.LENGTH_SHORT).show();
}
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = dbHelper.viewData();
if (res.getCount() == 0) {
Toast.makeText(getApplicationContext(), "No Data Found", Toast.LENGTH_SHORT).show();
}
StringBuffer sb = new StringBuffer();
while (res.moveToNext()) {
sb.append("ID: " + res.getString(0) + "\n");
sb.append("NAME: " + res.getString(1) + "\n");
sb.append("PHONE NUMBER: " + res.getString(2) + "\n\n");
}
showData("Details",sb.toString());
}
});
}
public void showData(String title, String mess) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(mess);
builder.show();
}
}