Can't do the insetion in database android - java

There is a problem with insetion in database. I don't know what to do.
It writes error inserting.
public class MainActivity extends Activity implements OnClickListener {
BaseOpener bo;
private static final String ML = "ML";
Button read, write;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
read = (Button) findViewById(R.id.read);
read.setOnClickListener(this);
write = (Button) findViewById(R.id.write);
write.setOnClickListener(this);
bo = new BaseOpener(this);
Log.i("Ml", "good start");
}
#Override
public void onClick(View v) {
SQLiteDatabase db = bo.getWritableDatabase();
switch (v.getId()) {
case R.id.write:
{
ContentValues cv = new ContentValues();
Log.i("ML", "write");
cv.put("id", 1);
cv.put("name", "Petr");
cv.put("phone", "911");
cv.clear();
db.insert("table1", null, cv);
}
break;
case R.id.read:
{`enter code here`
Log.i("ML", "read");
Cursor c = db.query("table1",null,null,null,null,null,null);
if(c.moveToFirst()){
int idColIndex = c.getColumnIndex("id");
int nameColIndex = c.getColumnIndex("name");
int emailColIndex = c.getColumnIndex("phone");
Log.i("ML",
"ID = " + c.getInt(idColIndex) +
", name = " + c.getString(nameColIndex) +
", phone = " + c.getString(emailColIndex));
}
}
break;
}
db.close();
Log.i("ML", "the base is closed");
}
}
public class BaseOpener extends SQLiteOpenHelper {
public BaseOpener(Context context) {
super(context, "contacts", null, 1);
Log.i("ML", "the base is ready");
}`enter code here`
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table table1 (" + "id integer primary key,"
+ "name String," + "phone String" + ");");
Log.i("ML", "table1 is ready");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}

clear()
Removes all values. Read more on ContentValues here

Your table lists id as primary key so you can't insert it twice. I would just comment out the cv.put("id",1) line and let sqlite handle it.
//cv.put("id", 1);
db.execSQL("create table table1 (" + "id integer primary key,"
+ "name String," + "phone String" + ");");
Log.i("ML", "table1 is ready");
Also from your next app on consider naming your id column _id. This will make the table more compatible with cursor adapters and such.
Look at these links they all use _id
cursorAdapter ContentProvider ListView

Related

Why do I fail to save data in my application?

I am a student and started working on android studio recently. I don't know about it much. I am working on an application where I save the item name and its amount in the database and display toast message if data we entered is saved or not. problem is whenever I click on the save button my application crashes.
following is my DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Items.db";
public static final String TABLE_NAME = "item_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "ITEM";
public static final String COL_3 = "AMOUNT";
public DatabaseHelper(#Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "ITEM TEXT, AMOUNT TEXT)";
db.execSQL(createTable);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS" + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item, String amount){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,item);
contentValues.put(COL_3,item);
long result = db.insert(TABLE_NAME,null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}
}
following is my MainActivity class:
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editItem, editAmount;
Button buttonSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editItem = (EditText)findViewById(R.id.item_field);
editAmount = (EditText)findViewById(R.id.amount_field);
buttonSave = (Button)findViewById(R.id.button_save);
AddData();
}
public void AddData(){
buttonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String item = editItem.getText().toString();
String amount = editAmount.getText().toString();
boolean insertData = myDb.addData(item, amount);
if(insertData == true){
Toast.makeText(MainActivity.this,"Amount is saved with Item detail", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "Error occurred : Detailed are not saved", Toast.LENGTH_LONG).show();
}
}
});
}
}
I will appreciate your help.
Thank you
It might be crashing because it's not creating the table:
String createTable = "CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "ITEM TEXT, AMOUNT TEXT)";
A space is missing between table and its name:
String createTable = "CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEM TEXT, AMOUNT TEXT)";
You should also close the database after you get the data in db.insert but this only creates a warning:
long result = db.insert(TABLE_NAME,null, contentValues);
db.close();

Data not being stored in SQLite database for some reason? Errors

i'm trying to store some data within a new my sql database and am getting logcat errors saying the table does not exist despite me creating a method to do this. Here is my code!
DataBaseHelper2.java -
package com.example.david.myview3;
/**
* Created by David on 23/03/2017.
*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper2 extends SQLiteOpenHelper {
public DataBaseHelper2(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
#Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE1);
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE2);
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE3);
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE4);
_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE5);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
#Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}
}
SurveyDataBaseAdapter -
package com.example.david.myview3;
/**
* Created by David on 23/03/2017.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class SurveyDataBaseAdapter {
static final String DATABASE_NAME = "survey.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE1 = "create table " + "USER" +
"( " + "ID" + " integer primary key autoincrement," + "ID int,NAME text); ";
static final String DATABASE_CREATE2 = "create table " + "QUESTION" +
"( " + "ID2" + " integer primary key autoincrement," + "QUESTION text,ANSWER1 text, ANSWER2 text, ANSWER3 text); ";
static final String DATABASE_CREATE3 = "create table " + "RESPONSE" +
"( " + "ID3" + " integer primary key autoincrement," + "ID int,QUESTION text, ANSWER int); ";
// Variable to hold the database instance
public SQLiteDatabase db2;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper2 dbHelper2;
public SurveyDataBaseAdapter(Context _context) {
context = _context;
dbHelper2= new DataBaseHelper2(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public SurveyDataBaseAdapter open() throws SQLException {
db2 = dbHelper2.getWritableDatabase();
return this;
}
public void close() {
db2.close();
}
public SQLiteDatabase getDatabaseInstance() {
return db2;
}
public void insertQuestion(String question, String answerOne, String answerTwo, String answerThree) {
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("QUESTION", question);
newValues.put("ANSWER1", answerOne);
newValues.put("ANSWER2", answerTwo);
newValues.put("ANSWER3", answerThree);
// Insert the row into your table
db2.insert("QUESTION", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
//new try
public void insertEntry2(String question, String answer) {
ContentValues newValues2 = new ContentValues();
// Assign values for each row.
newValues2.put("QUESTION", question);
newValues2.put("ANSWER", answer);
// Insert the row into your table
db2.insert("SURVEY1", null, newValues2);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName) {
//String id=String.valueOf(ID);
String where = "USERNAME=?";
int numberOFEntriesDeleted = db2.delete("LOGIN", where, new String[]{UserName});
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String[] getQuestion() {
final String TABLE_NAME = "QUESTION";
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getDatabaseInstance();
Cursor cursor = db2.rawQuery(selectQuery, null);
String[] data = null;
if(cursor.moveToFirst()) {
do {
// get the data into the array, or class variable
}while (cursor.moveToNext());
}
cursor.close();
return data;
}
public String getSingleEntry()
{
Cursor cursor=db2.query("QUESTION", null, null, null, null, null, null);
cursor.moveToFirst();
String question= cursor.getString(cursor.getColumnIndex("QUESTION"));
cursor.close();
return question;
}
public void updateEntry(String userName, String password) {
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD", password);
String where = "USERNAME = ?";
db2.update("LOGIN", updatedValues, where, new String[]{userName});
}
}
CreateSurveyActivity.java
package com.example.david.myview3;
/**
* Created by David on 23/03/2017.
*/
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
public class CreateSurveyActivity extends DashBoardAppActivity {
EditText editQuestion, editAnswer1, editAnswer2, editAnswer3;
Button btnNext, btnComplete;
SurveyDataBaseAdapter SurveyDataBaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photoalbum);
// get Instance of Database Adapter
SurveyDataBaseAdapter = new SurveyDataBaseAdapter(this);
SurveyDataBaseAdapter = SurveyDataBaseAdapter.open();
// Get References of Views
editQuestion = (EditText) findViewById(R.id.editTextQuestion);
editAnswer1 = (EditText) findViewById(R.id.editTextAnswer1);
editAnswer2 = (EditText) findViewById(R.id.editTextAnswer2);
editAnswer3 = (EditText) findViewById(R.id.editTextAnswer3);
// Get Refs of buttons
btnComplete = (Button) findViewById(R.id.buttonSignUP);
}
public void createSurvey(View V){
final Dialog dialog = new Dialog(CreateSurveyActivity.this);
dialog.setContentView(R.layout.photoalbum);
dialog.setTitle("Create");
// get the Refferences of views
final EditText editQuestion=(EditText)dialog.findViewById(R.id.editTextQuestion);
editQuestion.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
final EditText editAnswer1=(EditText)dialog.findViewById(R.id.editTextAnswer1);
editAnswer1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
final EditText editAnswer2=(EditText)dialog.findViewById(R.id.editTextAnswer2);
editAnswer2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
final EditText editAnswer3=(EditText)dialog.findViewById(R.id.editTextAnswer3);
editAnswer3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
// Set On ClickListener
Button btnNext = (Button) dialog.findViewById(R.id.buttonNext);
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String question = editQuestion.getText().toString();
String answer1 = editAnswer1.getText().toString();
String answer2 = editAnswer2.getText().toString();
String answer3 = editAnswer3.getText().toString();
// check if any of the fields are vaccant
if (question.equals("") || answer1.equals("") || answer2.equals("") || answer3.equals("")) {
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
{
// Save the Data in Database
SurveyDataBaseAdapter.insertQuestion(question, answer1, answer2, answer3);
Toast.makeText(getApplicationContext(), "Question Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
// try to send home on complete button press
//*
//btnNext = (Button) findViewById(R.id.buttonNext);
// btnNext.setOnClickListener(new View.OnClickListener() {
// final Intent intent = new Intent(context, DashHomeActivity.class);
//
// });
}
public void hideKeyboard(View view){
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SurveyDataBaseAdapter.close();
}
}
logcat -
04-17 20:26:41.408 14322-14322/com.example.david.myview3 E/SQLiteLog: (1) no such table: QUESTION
04-17 20:26:41.408 14322-14322/com.example.david.myview3 E/SQLiteDatabase: Error inserting ...
android.database.sqlite.SQLiteException: no such table: QUESTION (code 1): , while compiling: INSERT INTO QUESTION(QUESTION,ANSWER3,ANSWER2,ANSWER1) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:919)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:530)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1547)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1404)
at com.example.david.myview3.SurveyDataBaseAdapter.insertQuestion(SurveyDataBase Adapter.java:59)
at com.example.david.myview3.CreateSurveyActivity$5.onClick(CreateSurveyActivity .java:117)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19869)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:10 28)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Use SurveyDataBaseAdapter.DATABASE_CREATE1 instead of LoginDataBaseAdapter.DATABASE_CREATE1.
In your DataBaseHelper2.java, update onCreate() method as below:
#Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(SurveyDataBaseAdapter.DATABASE_CREATE1);
_db.execSQL(SurveyDataBaseAdapter.DATABASE_CREATE2);
_db.execSQL(SurveyDataBaseAdapter.DATABASE_CREATE3);
}
In your SurveyDataBaseAdapter , update SQL table create statement as below:
static final String DATABASE_CREATE1 = "CREATE TABLE " + "USER" + "( " +
"ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"ID1 INTEGER, " + "NAME TEXT " + ")";
static final String DATABASE_CREATE2 = "CREATE TABLE " + "QUESTION" + "( " +
"ID2 INTEGER PRIMARY KEY AUTOINCREMENT, " +
"QUESTION TEXT, " + "ANSWER1 TEXT, " + "ANSWER2 TEXT, " + "ANSWER3 TEXT " + ")";
static final String DATABASE_CREATE3 = "CREATE TABLE " + "RESPONSE" + "( " +
"ID3 INTEGER PRIMARY KEY AUTOINCREMENT, " +
"ID INTEGER, " + "QUESTION TEXT, " + "ANSWER INTEGER " + ")";

every time creating database after insert a row in android sql

#After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor
#After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor
#After insert a row Database is creating again .how can i solve this problem and i can check database is android device monitor
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int Database_version = 2;
public static final String Tag = DatabaseOperations.class.getSimpleName();
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TableData.TableInfo.TABLE_NAME + " (" +
TableData.TableInfo.USER_ID + " INTEGER PRIMARY KEY," +
TableData.TableInfo.USER_PASS +" TEXT "+ "," +
TableData.TableInfo.USER_EMAIL +" TEXT "+ ");";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null,Database_version);
Log.d("Tag", "Database created");
}
#Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(SQL_CREATE_ENTRIES);
Log.d("Tag", "Table created");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void putInformation(DatabaseOperations drop, String name, String pass, String email) {
SQLiteDatabase SQ = drop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_ID, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
Log.d("Tag", "inert a row");
}
public Cursor getInformation(DatabaseOperations dop) {
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] coloumns = {TableData.TableInfo.USER_ID, TableData.TableInfo.USER_PASS, TableData.TableInfo.USER_EMAIL};
Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME, coloumns, null, null, null, null, null);
return CR;
}
}
RegisterActivity
public class RegisterActivity extends AppCompatActivity {
EditText USER_NAME, USER_PASS, CON_PASS, USER_EMAIL;
String user_name, user_pass, con_pass, user_email;
Button REG;
Context ctx = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
USER_NAME = (EditText) findViewById(R.id.reg_user);
USER_PASS = (EditText) findViewById(R.id.reg_pass);
CON_PASS = (EditText) findViewById(R.id.con_pass);
USER_EMAIL = (EditText) findViewById(R.id.reg_email);
REG = (Button) findViewById(R.id.user_reg);
REG.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
user_name = USER_NAME.getText().toString();
user_pass = USER_PASS.getText().toString();
con_pass = CON_PASS.getText().toString();
user_email = USER_EMAIL.getText().toString();
if (!(user_pass.equals(con_pass))) {
Toast.makeText(getBaseContext(), "Password are not matching", Toast.LENGTH_LONG).show();
USER_NAME.setText("");
USER_PASS.setText("");
CON_PASS.setText("");
USER_EMAIL.setText("");
} else {
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB, user_name, user_pass, user_email);
Toast.makeText(getBaseContext(), "Registration is suessful", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
It is because you are creating table each time you insert a row. To solve this problem you need to change the create table query "CREATE TABLE 'TABLE_NAME' IF NOT EXISTS". The "IF NOT EXISTS" will restrict the system to create the database again if it is created once.
You are calling creating a new Table every time, you create an instance of DatabaseOperations - as your SQL statement 'CREATES'
Modify SQL_CREATE_ENTRIES to something like below
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE IF NOT EXISTS " + TableData.TableInfo.TABLE_NAME + " (" +
TableData.TableInfo.USER_ID + + " integer primary key autoincrement, "+
TableData.TableInfo.USER_PASS +" TEXT "+ "," +
TableData.TableInfo.USER_EMAIL +" TEXT "+ ");";
Edit
1) I've update query to include auto-increment your primary key
2) In your onUpgrade method add the line
db.execSQL("DROP TABLE IF EXISTS " + TableData.TableInfo.TABLE_NAME);
This will delete the table when you change the version of DB.
Next, update the version of by 1, to 3.
Re-run app, it will be rebuild the DB table.

NullPointerException when inserting into a database

I'm developing a program using database.
I'm stuck on inserting the data typed from EditText, keep on getting NullPointerException.
Here is my code:
AddBusDataActivity.java
public class AddBusDataActivity extends ActionBarActivity {
SQLiteDatabase mBusDatabse;
BusDatabaseHelper mBusDatabaseHelper;
EditText mNumber;
EditText mDestination;
EditText mArrivalTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
mBusDatabaseHelper = new BusDatabaseHelper(AddBusDataActivity.this, Constants.DATABASE_NAME, null, Constants.VERSION);
mNumber = (EditText)findViewById(R.id.number_text);
mDestination = (EditText)findViewById(R.id.destination_text);
mArrivalTime = (EditText)findViewById(R.id.arrival_time);
}
public void addBusActivityButton(View view) {
mBusDatabse = mBusDatabaseHelper.getWritableDatabase();
BusData bus = new BusData(mNumber.getText().toString(), mDestination.getText().toString(), Integer.valueOf(mArrivalTime.getText().toString()));
mBusDatabaseHelper.addBusData(bus); // error here
mBusDatabaseHelper.close();
Toast.makeText(AddBusDataActivity.this, "Bus data successfully added", Toast.LENGTH_SHORT).show();
this.finish();
}
}
BusDatabaseHelper.java
public class BusDatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase mBusDatabase;
public BusDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ Constants.TABLE_NAME + " (" + Constants.KEY_ID + " integer primary key autoincrement, " +
Constants.NUMBER + " text not null, " + Constants.DESTINATION + " text not null, " + Constants.ARRIVAL_TIME + " integer not null);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table " + Constants.TABLE_NAME + ";");
onCreate(db);
}
public void addBusData(BusData busData) {
ContentValues busValues = new ContentValues();
busValues.put(Constants.NUMBER, busData.getmNumber());
busValues.put(Constants.DESTINATION, busData.getmDestination());
busValues.put(Constants.ARRIVAL_TIME, busData.getmArrivalTime());
mBusDatabase.insert(Constants.TABLE_NAME, null, busValues); // error here
}
}
Error message
Caused by: java.lang.NullPointerException
at com.id11201478.exercise6.BusDatabaseHelper.addBusData(BusDatabaseHelper.java:36)
at com.id11201478.exercise6.AddBusDataActivity.addBusActivityButton(AddBusDataActivity.java:36)
I have separate Constants.java class for storing thoses values.
Thanks in advance!
It looks like mBusDatabase in BusDatabaseHelper is never initialised and therefore is null. Dereferencing it on line 36 causes your NPE.
You don't actually need it if you modify addBusData to grab it each time:
public void addBusData(BusData busData) {
ContentValues busValues = new ContentValues();
busValues.put(Constants.NUMBER, busData.getmNumber());
busValues.put(Constants.DESTINATION, busData.getmDestination());
busValues.put(Constants.ARRIVAL_TIME, busData.getmArrivalTime());
getWritableDatabase().insert(Constants.TABLE_NAME, null, busValues);
}
The variable
BusDatabaseHelper.mBusDatabase
is null. You need to initialize it!
Otherwise how does the system knows what mBusDatabase is
try to add this before insert
mBusDatabase= this.getWritableDatabase();

Unable to update a record in sqlite database

I am making this sample app that will insert, update, delete and retrieve data from database. now the problem I am facing is how to update the record. I know I have to pass Id but how.
MainActivity.java :
public class MainActivity extends Activity {
Button btnSubmit, btnUpdate, btnDelete;
EditText UserName, FirstName, LastName, txtPassword;
RunDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new RunDatabase(this);
UserName = (EditText) findViewById(R.id.User_Name);
FirstName = (EditText) findViewById(R.id.First_Name);
LastName = (EditText) findViewById(R.id.Last_Name);
txtPassword = (EditText) findViewById(R.id.Password);
btnSubmit = (Button) findViewById(R.id.btn_Submit);
btnSubmit.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v) {
addRow();
}
});
btnUpdate = (Button) findViewById(R.id.btn_Update);
btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateRow();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void addRow()
{
try
{
db.addRow
(
UserName.getText().toString(),
FirstName.getText().toString(),
LastName.getText().toString(),
txtPassword.getText().toString()
);
// remove all user input from the Activity
emptyFormFields();
}
catch (Exception e)
{
Log.e("Add Error", e.toString());
e.printStackTrace();
}
}
private void updateRow()
{
try
{
db.updateRow
(
UserName.getText().toString(),
FirstName.getText().toString(),
LastName.getText().toString(),
txtPassword.getText().toString()
);
emptyFormFields();
}
catch (Exception e)
{
Log.e("Update Error", e.toString());
e.printStackTrace();
}
}
private void emptyFormFields()
{
User_Id.setText("");
FirstName.setText("");
LastName.setText("");
UserName.setText("");
txtPassword.setText("");
}
}
RunDatabase.java:
public class RunDatabase {
Context context;
private SQLiteDatabase db; // a reference to the database manager class.
private final String DB_NAME = "Records"; // the name of our database
private final int DB_VERSION = 1; // the version of the database
// the names for our database columns
private final String TABLE_NAME = "tblRecords";
private final String TABLE_ROW_ID = "id";
private final String TABLE_ROW_ONE = "UserName";
private final String TABLE_ROW_TWO = "FirstName";
private final String TABLE_ROW_THREE = "LastName";
private final String TABLE_ROW_FOUR = "Password";
// the beginnings our SQLiteOpenHelper class
public void onCreate(SQLiteDatabase db)
{
String newTableQueryString =
"create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null, " +
TABLE_ROW_ONE + " text, " +
TABLE_ROW_TWO + " text, " +
TABLE_ROW_THREE + " text, " +
TABLE_ROW_FOUR + " text" +
");";
System.out.println(newTableQueryString);
// execute the query string to the database.
db.execSQL(newTableQueryString);
}
public RunDatabase(Context context)
{
this.context = context;
// create or open the database
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addRow(String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
values.put(TABLE_ROW_THREE, rowStringThree);
values.put(TABLE_ROW_FOUR, rowStringFour);
try
{
db.insert(TABLE_NAME, null, values);
}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace(); // prints the stack trace to the log
}
}
public void updateRow(long rowID, String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
values.put(TABLE_ROW_THREE, rowStringThree);
values.put(TABLE_ROW_FOUR, rowStringFour);
try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}
}
1. In your create table String,
String newTableQueryString =
"create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null, " +
TABLE_ROW_ONE + " text, " +
TABLE_ROW_TWO + " text, " +
TABLE_ROW_THREE + " text, " +
TABLE_ROW_FOUR + " text" +
");";
The ROW_ID is an integer, so in your function updateRow(), the data type for parameter of rowID should be integer, which is passed in the "where" clause. So should change
-> public void updateRow(long rowID, String rowStringOne,...
2. To identify the row you have added, you need to have a primary key which you will be able to identify on the basis of the entry.
Auto increment integer is good for ensuring that the database will have a new row each time,
but the primary key you need should be something you will know to differentiate entries.
Can try a derived key which would be another column in your db, to insert the same with adding rows would be like:
String customPrimaryKey="UserName.getText().toString()+FirstName.getText().toString()+txtPassword.getText().toString()";
db.updateRow
(
UserName.getText().toString(),
FirstName.getText().toString(),
LastName.getText().toString(),
txtPassword.getText().toString(),
// add one more value for a particular entry-a primary key for You to identify that row
customPrimaryKey
);
So you have a way to identify which row to update then, and pass the same in the updateRow() parameters for the where clause
You could try like this-
db.update(TABLE_NAME, values, TABLE_ROW_ID + "=?", String[]{rowID});

Categories

Resources