I made a note app and I give users an option to leave the title of their note empty. But in RecyclerView and Toolbar title is shown and in this case it's empty. I tried to give value to that column while creating the table but it doesn't work.
Here's the code to create table:
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + DATABASE_TABLE + "(" +
ID + " INTEGER PRIMARY KEY," +
TITLE + " TEXT DEFAULT 'No Title'," +
CONTENT + " TEXT," +
DATE + " TEXT," +
TIME + " TEXT" + ")";
db.execSQL(query);
}
UPDATE:
Save method in adding note:
private void saveNote() {
if (TextUtils.isEmpty(title.getText()) && TextUtils.isEmpty(content.getText())) {
Toast.makeText(this, "Please write Something in your note", Toast.LENGTH_SHORT).show();
} else {
NotePad notePad = new NotePad(title.getText().toString(), content.getText().toString(), currentDate, currentTime);
Database database = new Database(this);
database.addNote(notePad);
Toast.makeText(this, "Note Saved!", Toast.LENGTH_LONG).show();
moveToList();
}
}
add note method in database:
void addNote(NotePad notePad) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TITLE, notePad.getTitle());
cv.put(CONTENT, notePad.getContent());
cv.put(DATE, notePad.getDate());
cv.put(TIME, notePad.getTime());
database.insert(DATABASE_TABLE, null, cv);
}
In the method addNote() you should not set a value in the ContentValues object for TITLE if it is empty, so SQLite will set the column's value to the default value:
void addNote(NotePad notePad) {
SQLiteDatabase database = this.getWritableDatabase();
cv = new ContentValues();
if (!TextUtils.isEmpty(notePad.getTitle())) cv.put(TITLE, notePad.getTitle());
cv.put(CONTENT, notePad.getContent());
cv.put(DATE, notePad.getDate());
cv.put(TIME, notePad.getTime());
database.insert(DATABASE_TABLE, null, cv);
}
Use IFNULL while fetching data . For example
Select IFNULL(“TITLE”,”NO TITLE”) from table...
Related
This might be impossible but I couldn't seem to find a clear answer. When I delete a row in my database I want the other row's IDs to essentially move up, so if I deleted row 2, then row 3's ID would become 2. Is this possible? I am using AUTOINCREMENT so didn't know if there was almost a reverse of that?
Here is my full SQLite Code.
public class ProfileDatabaseHelper extends SQLiteOpenHelper {
public static final String PROFILE_TABLE = "PROFILE_TABLE";
public static final String PROFILE_ID = "ID";
public static final String PROFILE_IMAGE = "PROFILE_IMAGE";
public static final String RADAR_DATA_ONE = "DATA_ONE";
public static final String RADAR_DATA_TWO = "DATA_TWO";
public static final String RADAR_DATA_THREE = "DATA_THREE";
public static final String RADAR_DATA_FOUR = "DATA_FOUR";
public static final String RADAR_DATA_FIVE = "DATA_FIVE";
public static final String RADAR_DATA_SIX = "DATA_SIX";
public ProfileDatabaseHelper(#Nullable Context context) {
super(context, "profiles.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTableStatement = "CREATE TABLE " + PROFILE_TABLE + " (" + PROFILE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + PROFILE_IMAGE + " TEXT, "
+ RADAR_DATA_ONE + " INT, " + RADAR_DATA_TWO + " INT, " + RADAR_DATA_THREE + " INT, " + RADAR_DATA_FOUR + " INT, " + RADAR_DATA_FIVE
+ " INT, " + RADAR_DATA_SIX + " INT)";
db.execSQL(createTableStatement);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean updateData(Integer id,String profilePhoto,Integer dataOne, Integer dataTwo, Integer dataThree, Integer dataFour, Integer dataFive, Integer dataSix){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(PROFILE_ID,id);
contentValues.put(PROFILE_IMAGE,profilePhoto);
contentValues.put(RADAR_DATA_ONE,dataOne);
contentValues.put(RADAR_DATA_TWO,dataTwo);
contentValues.put(RADAR_DATA_THREE,dataThree);
contentValues.put(RADAR_DATA_FOUR,dataFour);
contentValues.put(RADAR_DATA_FIVE,dataFive);
contentValues.put(RADAR_DATA_SIX,dataSix);
db.update(PROFILE_TABLE,contentValues,"ID = ?",new String[] {id.toString()});
return true;
}
public boolean addOne(ProfileModel profileModel){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(PROFILE_IMAGE, profileModel.getProfilePhoto());
cv.put(RADAR_DATA_ONE, profileModel.getDataOne());
cv.put(RADAR_DATA_TWO, profileModel.getDataTwo());
cv.put(RADAR_DATA_THREE, profileModel.getDataThree());
cv.put(RADAR_DATA_FOUR, profileModel.getDataFour());
cv.put(RADAR_DATA_FIVE, profileModel.getDataFive());
cv.put(RADAR_DATA_SIX, profileModel.getDataSix());
long insert = db.insert(PROFILE_TABLE, null, cv);
if (insert == -1){
return false;
}
else{
return true;
}
}
public Cursor alldata(){
SQLiteDatabase dataBaseHelper = this.getWritableDatabase();
Cursor cursor = dataBaseHelper.rawQuery("select * from PROFILE_TABLE ", null);
return cursor;
}
public boolean delete(int id) {
SQLiteDatabase db = this.getWritableDatabase();
String queryString = "DELETE FROM " + PROFILE_TABLE + " WHERE " + PROFILE_ID + " = " + id;
//deleting row
Cursor cursor = db.rawQuery(queryString, null);
if(cursor.moveToFirst()){
return true;
}
else {
return false;
}
}
}
I am using AUTOINCREMENT so didn't know if there was almost a reverse of that?
First AUTOINCREMENT doesn't increase the rowid (or alias thereof) value rather it is a constraint (rule) that says that the rowid MUST be greater than any that have ever been allocated (if sqlite_sequence hasn't been modified outside of SQLite's management of the table).
It is using INTEGER PRIMARY KEY that allows a value, typically 1 greater than the highest current rowid value, to be automatically assigned. However, if the value + 1 is greater than the maximum possible value (9223372036854775807) then :-
With AUTOINCREMENT you get an SQLITE_FULL error.
Without AUTOINCREMENT attempts are made to find an unused number.
It is extremely unlikely that (9223372036854775807) will be reached/used.
AUTOINCREMENT is less efficient as it has to record the highest ever assigned rowid and does so by using the sqlite_sequence table. In the SQLite documentation it says :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.
see SQLite Autoincrement
It is a very bad idea to utilise the rowid or an alias thereof for anything other than it's intended use that is for unique identifying a row from another row such as when forming a relationship, updating or deleting a row.
e.g. what if you sort (ORDER BY) the data by another column or columns other than the ID column? Does the id have any meaning to a user of the App?
However, even though this it NOT recommended, the following would do what you wish :-
private void rationaliseCol1Values() {
ContentValues cv = new ContentValues();
Cursor csr = mDB.query(PROFILE_TABLE,null,null,null,null,null,PROFILE_ID + " ASC");
int rowcount = csr.getCount();
long expected_id = 1;
long current_id;
String where_clause = PROFILE_ID + "=?";
String[] args = new String[1];
while (csr.moveToNext()) {
current_id = csr.getLong(csr.getColumnIndex(PROFILE_ID));
if (current_id != expected_id) {
cv.clear();
cv.put(PROFILE_ID,expected_id);
args[0] = String.valueOf(current_id);
mDB.update(PROFILE_TABLE,cv,where_clause,args);
}
expected_id++;
}
csr.close();
// Now adjust sqlite_sequence
where_clause = "name=?";
args[0] = PROFILE_TABLE;
cv.clear();
cv.put("seq",String.valueOf(rowcount));
mDB.update("sqlite_sequence",cv,where_clause,args);
}
Note the code has been taken from the answer here Android Studio Sqllite autoincrement reset
and has been amended to suit but has not been compiled or run and therefore may contain some errors.
I am getting this error when trying to read from the SQLite DB
IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
DbHelper dbHelper = new DbHelper(this);
SQLiteDatabase database = dbHelper.getWritableDatabase();
Cursor cursor = dbHelper.readNumber(database);
String ItemId;
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
//getting the ERROR here
ItemId = cursor.getString(cursor.getColumnIndex(DbContract.ITEMID));
number = cursor.getString(cursor.getColumnIndex(DbContract.PHONE_NUMBER));
callType = cursor.getString(cursor.getColumnIndex(DbContract.CALL_TYPE));
callDate = cursor.getString(cursor.getColumnIndex(DbContract.CALL_DATE));
callDuration = cursor.getString(cursor.getColumnIndex(DbContract.CALL_DURATION));
arrayList.add(new PhNumber(ItemId, number, callType, callDate, callDuration));
if (debug) {
Log.i(TAG, "DATA FOUND IN DB:\n" + "\t ID: " + ItemId + ", Number: " + number + "\n");
}
}
cursor.close();
dbHelper.close();
result = true;
if (debug) {
Log.d(TAG, " Number of items in DB: " + arrayList.size());
}
}
readNumber
public Cursor readNumber(SQLiteDatabase database) {
String[] projections = {"id", DbContract.PHONE_NUMBER};
return (database.query(DbContract.TABLE_NAME, projections, null, null, null, null, null));
}
This is my DB
private static final String CREATE = "create table " + DbContract.TABLE_NAME +
"(id integer primary key autoincrement,"
+ DbContract.ITEMID + " text, "
+ DbContract.PHONE_NUMBER + " text, "
+ DbContract.CALL_TYPE + " text, "
+ DbContract.CALL_DATE + " text, "
+ DbContract.CALL_DURATION + " text, "
+ DbContract.SYNC_STATUS + " text)";
In your projection, created by readNumber call, you have only the id and PHONE_NUMBER columns returned. Probably DbContract.ITEMID is not equal to id and when trying to lookup the DbContract.ITEMID in the cursor it is not found. To fix this you need to use ITEMID in readNumber method, something like:
public Cursor readNumber(SQLiteDatabase database) {
String[] projections = {DbContract.ITEMID, DbContract.PHONE_NUMBER};
return (database.query(DbContract.TABLE_NAME, projections, null, null, null, null, null));
}
Another issue is that you are trying to access other fields too, like: CALL_TYPE, CALL_DATE, etc.
So, in order to fix the issue you either:
Do not try to retrieve the fields that are not part of the result.
Add the needed fields in the projection too.
Found the issue:
I was trying to access the columns that were not added in the projects in readNumber method adding those projections solved the issue.
readNumber
public Cursor readNumber(SQLiteDatabase database) {
String[] projections = {
DbContract.ITEMID,
DbContract.PHONE_NUMBER,
DbContract.CALL_TYPE,
DbContract.CALL_DATE,
DbContract.CALL_DURATION};
return (database.query(DbContract.TABLE_NAME, projections, null, null, null, null, null));
}
contactid = 123;
SYNC_SUCCESS = 1;
db.updateSyncStatus(contactid, SYNC_SUCCESS);
I have tried the 3 possible ways to update the table in SQLite DB. But its not working. INSERT and DELETE process are working good. Only I am facing problem in the UPDATE. Did I missed anything?
public void updateSyncStatus(String contactid, int syncSuccess) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues CV = new ContentValues();
CV.put(CONTACTS_SYNC_STATUS, syncSuccess);
try {
// db.update(TABLE_CONTACTS, CV, CONTACTS_CONTACTID + "='" + contactid + "'", null); // Tried, Not working
// db.update(TABLE_CONTACTS, CV, CONTACTS_CONTACTID +" = ?", new String[] {contactid}); // Tried, Not Working
db.update(TABLE_CONTACTS, CV, CONTACTS_CONTACTID + " = ?", new String[]{contactid});
}
catch (Exception e){
String error = e.getMessage().toString();
Log.e(TAG, "UpdateError: " + error);
}
db.close();
}
Table Structure:
String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CONTACTS + "("
+ CONTACTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ CONTACTS_NUMBER + " VARCHAR,"
+ CONTACTS_CONTACTID + " VARCHAR,"
+ CONTACTS_SYNC_STATUS + " TINYINT DEFAULT 0" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
Actually the problem is not with the update query. The problem is , before executing the updateSyncStatus method, next statement ran and I am getting the output before updating the rows. So I have used the Handler to wait for 10 seconds before showing the output.
I have a string called str_train_no and i want to create a database with the same name and so far i have tried
String name=str_Train_no+".db";
SQLiteDatabase traindb = openOrCreateDatabase(name,SQLiteDatabase.CREATE_IF_NECESSARY , null);
sql="CREATE TABLE IF NOT EXISTS "+str_Train_no+" ("
+"Stops VARCHAR);";
traindb.execSQL(sql);
Heres the Source code for the database declaration and insertion part
SQLiteDatabase db = openOrCreateDatabase( "Train_list.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
try {
final String CREATE_TABLE_TRAIN_LIST = "CREATE TABLE IF NOT EXISTS Train_list ("
+ "Train_name VARCHAR,"
+ "Train_no VARCHAR,"
+ "Train_start VARCHAR,"
+ "Train_end VARCHAR,"
+ "Seats_Available VARCHAR);";
db.execSQL(CREATE_TABLE_TRAIN_LIST);
Toast.makeText(admin_manipulation.this, "Table created", Toast.LENGTH_LONG).show();
String sql = "INSERT or replace INTO Train_list (Train_name, Train_no, Train_start, Train_end, Seats_Available) VALUES('"+str_Train_name + "',' " +str_Train_no + "', '" +str_Train_start+"','" +str_Train_end+"',' " +str_Train_seats +"');";
db.execSQL(sql);
Toast.makeText(admin_manipulation.this, "Inserted Sucessfully", Toast.LENGTH_SHORT).show();
String name=str_Train_no+".db";
SQLiteDatabase traindb = openOrCreateDatabase(name, SQLiteDatabase.CREATE_IF_NECESSARY , null);
sql="CREATE TABLE IF NOT EXISTS "+str_Train_no+" ("
+"Stops VARCHAR);";
traindb.execSQL(sql);
Toast.makeText(admin_manipulation.this, "Table "+str_Train_no+" verified", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(admin_manipulation.this, "An Error has occured", Toast.LENGTH_SHORT).show();
}
and this keeps throwing an exception.
Any ideas why?
Thanks in advance!
With str_train_no beeing a numerical string you have the problem that you try to create a table with a name which is no valid SQL identifier.
In this particular case you can solve it by prefixing it with a text string like:
sql = "CREATE TABLE IF NOT EXISTS train"+str_Train_no+ ...
In your case you can also use a static table name, since you create one database per train (which might not be the best thing to do).
use text instead of varchar. as I know there is not varchar in SQLite.
I am learing to code and made a stopwatch that saves the laptimes in a String and gets the date as a String as well. I want to put those in a SQLite database (so i can later display the date in a listview and open it in another activity that shows all the laptimes.) I've followed some of the codes on the internet and try to put stuff together so I might look over some things in my code. I've commented on the parts I think i understand so you can follow my thinking a bit.
The problem: when i press save the following code is executed and returns the toastmessage: Somehting went wrong.
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String dateStamp = getCurrentTimeStamp();
AddData(dataInput, dateStamp);
//DatabaseHelper.deleteAll();
}
}); //save data though AddData method as input the listText
The method AddData is as follows:
public void AddData(String time, String date) {
boolean insertData = DatabaseHelper.addData(time, date);
if (insertData) {
toastMessage(dataInput);
} else {
toastMessage("Something went wrong");
}
}
The boolean method in the DatabaseHelper class is this:
public boolean addData(String times, String date) { //addData that takes a string
SQLiteDatabase db = this.getWritableDatabase(); //database called db and use getWritableDatabase method
ContentValues contentValues = new ContentValues(); //make a new object of ContentValues
contentValues.put(COL_2, times); //put COL_2 and the String in the ContentValues object
contentValues.put(COL_3, date);
long result = db.insert(TABLE_NAME, null, contentValues); //insert contentValues object into the table
//if date as inserted incorrectly it will return -1
if (result == -1) {
db.close();
return false;
} else {
return true;
}
}
It works when i input just 1 variable in addData() but not with 2 that I later implemented. I think it should work. Below I also put the code that is used to make the SQLite Database.
public static final String TABLE_NAME = "stopwatch"; //make a table with name
public static final String COL_1 = "ID"; //make an ID for every colomn
public static final String COL_2 = "times"; //make a 2nd colomn for data
public static final String COL_3= "date";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) { //make the onCreate method that takes the database as input
String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_2 +" TEXT" + COL_3 +" TEXT)"; //create the table with SQL statements to input the data correctly
db.execSQL(createTable); //input the SQL statements in the DB
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //make upgrade method that takes the database and the versions
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME); //execute SQL statements drop table and which one
onCreate(db); //run through create method
}
I hope someone can help me to find the problem so I can learn more.
Your create table query is missing a comma after the COL_2 +" TEXT"
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_2 +" TEXT, " + COL_3 +" TEXT)"; // Added a comma after the COL_2
db.execSQL(createTable);
}
I can see one problem from a quick glance and that is a missing comma in your creation statement which would mean that your database was not created as you intended. Try the below amendment.
String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_2 +" TEXT," + COL_3 +" TEXT)";