我创建了一个数据库“ contact.db”,其表名为“ contact”。我有ID,姓名,电子邮件和手机号码字段(ID是自动生成的)。该应用程序也已成功运行,当我输入所有详细信息并单击“保存”时,吐司显示“已插入数据”,但是,如果我将数据库导出到本地计算机并看到表,则数据未插入其中。
Mainactivity.java
package org.pankaj.com.contactmanager;
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 {
DatabaseHelper databaseHelper;
Button insert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper=new DatabaseHelper(this);
insert= findViewById(R.id.btnAdd);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,AddNewContact.class);
startActivity(intent);
}
});
}
}
Addnewcontact.java
package org.pankaj.com.contactmanager;
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 AddNewContact extends AppCompatActivity {
EditText name,mobile_number,email;
Button save;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_contact);
databaseHelper=new DatabaseHelper(this);
name=findViewById(R.id.etName);
mobile_number= findViewById(R.id.etMobileNumber);
email=findViewById(R.id.etEmailId);
save=findViewById(R.id.btnSave);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean result=databaseHelper.insertData(name.getText().toString(),Integer.parseInt(mobile_number.getText().toString()),email.getText().toString());
if(result)
Toast.makeText(AddNewContact.this,"data inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(AddNewContact.this,"data not inserted", Toast.LENGTH_SHORT).show();
}
});
}
}
Databasehelper.java
package org.pankaj.com.contactmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="contact.db";
private static final String TABLE_NAME="contact";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
//SQLiteDatabase db=this.getWritableDatabase(); // used only for database creation
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MOBILE_NUMBER INTEGER,EMAIL TEXT)");
db.execSQL("commit;");
}
@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, int mobile_number, String email){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("NAME",name);
contentValues.put("MOBILE_NUMBER",mobile_number);
contentValues.put("EMAIL",email);
long result= db.insert(TABLE_NAME,null,contentValues);
// db.execSQL("commit;");
if(result == -1)
return false;
else
return true;
}
}