Data insertion failed after wiped out app data emulator - android

Inserting data in SQlite from EditText
Code was working well before i wiped out data from emulator now its giving me Null Pointer Exception, Kindly figure where actual problem lies. Thanks
Data is being inserted from here ]
public long insert(String name, String password, String role, String unique) {
sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_USERNAME, name);
contentValues.put(KEY_PASSWORD, password);
contentValues.put(KEY_ROLE, role);
contentValues.put(KEY_UNIQUE, unique);
long result = sqLiteDatabase.insert(DOCTOR_TABLE,
null,
contentValues);
sqLiteDatabase.close();
return result;
}
SQLite Database class

Well. It's an obvious.
As you mentioned you have wiped the data, so the table which is located in the android app's own data folder will be deleted too. So you must check whether it exists or not.
Call to Getting your table returns null.
So you inserting your value to nothing.
So You can just alter the code to see if the table exists and if not create it.
Basically you just call,
CREATE TABLE IF NOT EXISTS method(SQLite 3.3 and above support IF NOT EXISTS). When you first call it, if not existed, it will create that table but later it will return the existing table with same name.
In one line- check or get the correct table before you do any operation.

Related

How to update a value in a column with existing value in SQLite?

I'm trying to update a table in SQLite android. I have a column called 'quantity' which stores qty of some items, say item1, item2 ...
Now when I purchase item1, I'd definitely want to 'add' the purchased qty to an existing qty of item1.
I searched the web but couldn't find a solution, hence asking this.
My simple code's below:
// This method is used to 'UPDATE' the table 'stock'.
// This method will be used by two fragments,
// 'sale' and 'purchase' fragments.
public int updateData(String cigaretteName,int quantity, int cost, int totalCost) {
// Accessing the database with writable functionality so it can be updated.
SQLiteDatabase db = this.getWritableDatabase();
// Creating content values object to put the new values in existing rows with old values.
ContentValues contentValues = new ContentValues();
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
contentValues.put(StockEntry.COLUMN_COST, cost);
contentValues.put(StockEntry.COLUMN_TOTAL_COST, totalCost);
// Which row to update, based on the cigarette name.
String selection = StockEntry.COLUMN_CIGARETTES_NAME + " LIKE ?";
String[] selectionArgs = {cigaretteName};
// Updating the table with the new values and then returning the number of rows affected.
return db.update(StockEntry.TABLE_NAME, contentValues, selection, selectionArgs);
}
This isn't working at all, now it doesn't even update the column/row.
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
Do help guys!
I would suggest simple approach to overcome these kind of SQLite related issues.
Use SQLite Manager which is plugin for FireFox browser
Download from https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Create your dummy database there
Perform your CRUD operations here
Once everything working fine in SQLite Manager then use same query inside your project.
Above way will save your development time as well as testing.

How to add a value after being logged in?

I am working in an android studio project and I have a login activity,I created database using SharedPreferences and SQLite;I have created a table which has columns id,email,password,check_in,check_out.
I made it possible to login after I register a user;but I do not know how to insert a data in check_in cell of logged user.
So my problem is that how to make possible to add the time of check_in in table in database after logged in?
And how I can the see the table created using SQLite?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("check_in", value);
contentValues.put("check_out", value);
// this will insert if record is new, update otherwise
db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
I made it possible to login after I register a user;but I do not know
how to insert a data in check_in cell of logged user.
You would use the SQLiteDatabase's update method something along the lines of :-
ContentValues cv = new ContentValues();
cv.put("check_in",System.currentTimeMillis());
SQLiteDatabase db = this.getWritableDatabase();
db.update("your_table_name",cv,"email=? AND user=?",new String[]{email,user});
And how I can the see the table created using SQLite?
You can
install a product that provides this ability,
copy the database via adb or tools that utilise adb, so that a product that can look at SQLite databases can access the copied file,
extract a cursor from the table and then use this as the source for displaying the data e.g you could use something like
:-
SQLiteDatabae db = this.getWriteableDatabase();
Cursor csr = db.query("your_table_name",null,null,null,null,null,null);
Databaseutils.dumpCursor(csr);
csr.close();

updating record in android programming

I am writing a program to display the details of students' record in android. I am using a existing database and getting the data from it. Here I need to update the status as confirmed.
I have created a confirmation button which on pressing will update the status as confirmed. Here on pressing the button my code shows no error but the value is not updated in the database. For information I am using sqlitebrowser as my database.
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
//contentValues.put(DatabaseHelper.STATUS,"CONFIRMED");
StudentDetails = SQLiteDatabase.openDatabase("", null, SQLiteDatabase.OPEN_READWRITE);
//status.setText("CONFIRMED");
//mDbHelper.close();
values.put("Status", "Confrimed");
StudentDetails.insert("Student","", values);
Toast.makeText(getApplicationContext(), "Status upadated", Toast.LENGTH_LONG).show();
StudentDetails.close();
}
}
I noticed you've supplied "" as the argument to path for openDatabase(). Unless you're calling this on a member that will open the appropriate database, I believe this wouldn't open a valid database, and thus I wouldn't be surprised if a row didn't get written to the table you were trying to modify. Perhaps try supplying a database file name instead of a blank string and see if that works.
Also, if you're not implementing SQLiteOpenHelper in your own class as outlined in Saving Data in SQL Databases, I would suggest trying that instead of accessing the database in another way, if it isn't too much trouble. I've just made an app using this method, and I have no problems.
Nonetheless, I would assign a long to the output value of the insert function, and set a debug point or write its value of the console. If the value is -1, the method didn't assign a row to the table (possibly because of database constraints - perhaps check that you're satisfying them in terms of the values supplied in the Bundle).
The value returned from insert will be -1 if an error occurred, as outlined in the docs for insert(String table, String nullColumnHack, ContentValues values)).
You should use StudentDetails.update("Student","", values);

Set EditText as a constant n db

Is it possible to take data from a edit text field and insert it into a SQLite db as a constant. I'm am trying to insert data into a SQLite primary key table and need for that data to be a constant for the db to accept it. I can't find any examples that would how me how to accomplish this.
i gotta say, i'm not quite sure what you mean by constant. just about every tutorial advises that you insert into the database with this method:
ContentValues values = new ContentValues();
values.put(DB_COLUMN, EtString);
long id = db.insert(DB_TABLE, null, values);
EtString is the string you get from the edittext field (with .getText, .toString or otherwise). This method would also provide a id for the string in the table that you could reference. But just know that if there's an _id field already in your database table, each row of information already gets an id without you doing anything.
Here's a good tutorial for this stuff:
http://www.vogella.com/articles/AndroidSQLite/article.html

Is it possible to apply key's (Primary key, candidate key etc) in android while using database

I am new to android and i have just learned how to use the database and i have a couple of questions:
How can we apply keys on the data in the database in android just like we give in Oracle database? Is it possible? If no please tell me why.
When i am deleting the first row in a database (Whose id is '1') the below rows id's are not coming in serial number i.e., id for the second row (Now first row) is '2' why not '1'.
Thank you
ok, from first,
"How can we apply keys on the data in the database in android just like we give in Oracle database? Is it possible? If no please tell me why."
Ans - We can apply the all rule which one applies to a any other database, like oracle, mysql etc... So you can have both concept of primary key and foreign key in Android's SQLite database.
2.When i am deleting the first row in a database (Whose id is '1') the below rows id's are not coming in serial number i.e., id for the second row (Now first row) is '2' why not '1'?
Ans : as per the database rules whenever you delete any records from row then its key is remains same, its not changed and whatever data after that records are also has a same key or ID so whenever you want to access that data the ID or key remain same.
EDIT: and If you want to modify that key or ID you can use UPDATE query for that.
EDIT: update(String table, ContentValues values, String whereClause, String[] whereArgs)
Updating Values
To execute an update statement, we have two ways:
1. To execute db.execSQL
2. To execute db.update method:
public int UpdateEmp(Employee emp)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(colName, emp.getName());
cv.put(colAge, emp.getAge());
cv.put(colDept, emp.getDept());
return db.update(employeeTable, cv, colID+"=?",
new String []{String.valueOf(emp.getID())});
}
The update method has the following parameters:
1. String Table: The table to update a value in
2. ContentValues cv: The content values object that has the new values
3. String where clause: The WHERE clause to specify which record to update
4. String[] args: The arguments of the WHERE clause
Convenience method for updating rows in the database.
Hope you will understand it.
Thanks,

Categories

Resources