How to update query using SQLite with cast as int - android

I'm having an error on using update with using cast (column1 as int) query. I need a VARCHAR column for our data seurity (encrypting our data). I can't find an answer on google. Please help me. Thanks

If your column1 were declared as VARCHAR, then most likely its affinity means that the actual underlying storage class in SQLite is TEXT. If you have the need to store integers, then you should have declared this column as INT or INTEGER. That being said, the following update might work:
UPDATE tickets SET is_send = 1;
This would work if SQLite can do the conversion from integer to string for you. If not, then use:
UPDATE tickets SET is_send = '1';
Or, if you want to cast, then use:
UPDATE tickets SET is_send = CAST('1' AS TEXT);
In general, if you have the need to store numbers, use a number column, and vice-versa for text.

Related

Sugar Orm just return the default value after saving new data in the new added column(upgrading Sugar Orm)

I want to add a new column anotherValue to my table AnotherTable.
I have read this related question and the SugarORM documentation, but when I save new data, and want to get anotherValue later, it just returns 0.
The datatype needs to be INT, not integer, and you need the COLUMN keyword:
ALTER TABLE another_table ADD COLUMN another_value INT;
I actually wrote the (accepted) answer you linked to, I suggest you follow it more closely!
If you're still having trouble, please turn on SugarORM logging and post what is output when you save & load data. Additionally, try using a table & column name without underscore, to rule out SugarORM's name conversion causing issues.

ORMLite change generatedId value

I'm using ORMLite in my android app and I searched a lot but I don't have a clear way to do what I want. I would like to know if I can change the starting value of a generatedId value. For example now it starts in 1 and I would like to start in 010001 or 020001. I noticed that we have sqlite_sequence table inside. Must I change that value or must I do it in another way?.
Thank you very much!
For example now it starts in 1 and I would like to start in 010001 or 020001.
In general I take the opinion that you should not do something like this. The id field is designed to be an identity and your application should not depend on the values being anything particular.
If you need some specific number then I would generate it externally to the database and add it as another field on the items. So the id can float and be whatever the DB wants and you can have your special ID that you control.
Finally I solved it. Here is the trick:
#DatabaseField(allowGeneratedIdInsert = true, generatedId = true, columnName = ID)
private int id;
I allow generatedIdInsert so I can set manually my Id and if I don't generate it manually it will be autogenerated. After that I do a first explicit insert.
daoObject.setId(010000);
dao.create(daoObject);
Now when I insert a new object the generatedId will start in 010001.
I hope that this little trick help somebody.
You have to have the AUTO_INCREMENT = 0100001; in your CREATE TABLE but as this is most certainly generated code by ORMLite you would have to force it by using the ORMLite annotation.
#DatabaseField(generatedId = true,
columnDefinition = "INTEGER PRIMARY KEY AUTOINCREMENT = 0100001")
private int id;

Column index order SQLite creates table

This is the query that I use to create a table
create table site_table(
_id integer primary key autoincrement,
name_site text,
url text,
login text,
pass text
);
I called Cursor.getColumnNames() and noticed that columns order are id, login, pass, name, url.
So, if I want a value I have to get it by the index Cursor.getString(index). Until I debugged I was messing up calling the wrong index, but now I wonder, why SQLite saves that way? Why it does not follow that way I created id, name_site, url, login and pass?
Thanks
So, if I want a value I have to get it by the index
Cursor.getString(index)
So for example for this reason you should always use
c.getString(c.getColumnIndex("ColName")); // or better getColumnIndex(CONSTANT)
This method saves all of us and ensure that you never get wrong results. Generally this method is recommended and also storing COLUMN_NAMES as CONSTANTS in separated class is very, very useful and efficient practise.
Note: Order depends on projection i.e. select name, lastname from table
That data is ordered by the order your requested it in your query, not the order you created the table with. So you probably changed the order in your query that generated said cursor.
Columns order in your cursor depends on projection. To be sure you use correct column index use c.getString(c.getColumnIndexOrThrow("COLUMN_NAME")) where c is your cursor.
I just made the experience first hand:
The indices of the columns of the cursor as a result of a
SELECT * FROM mytable WHERE ...
query have sometimes (not always) a different order that what SQLITE Database Browser shows as column order in the Database Structure tab. So referencing the columns via getColumnIndex seems to be the only safe way.

Android - how to use "select last_insert_rowid();"

I'm newbie in Android.
I want to display a new ID in the TextView.
So, I just think of getting latest ID that had been store in the database and declare as Integer add 1 to the value that I get then display to the TextView.
I have read many of the question regarding the getting the latest ID. How can I use select last_insert_rowid();?
Thanks!
last_insert_rowid() works only for records that have been inserted in the same session.
If your column is declared as INTEGER PRIMARY KEY, then SQLite will automatically generate a value for it if you don't specify one in a new record.
If you really need the ID before you have inserted the record, you can execute something like this:
SELECT max(_id) FROM MyTable
if you use autoincrement use
SELECT * from SQLITE_SEQUENCE;
to get the latest id.
Cursor c = database.rawQuery("SELECT last_insert_rowid()", null);
c.moveToFirst();
int id = c.getInt(0);
id += 1;
I'm a newbie too so can't explain very well. The above will get the last insert id from the same session. It won't work if a new session is started, ie you insert something and close the connection and reopen it, as it will then return 0 so you'll need to bear that in mind as your TextView would always show 1. As like you I read many questions about it without knowing how to implement it. The above code is how I managed to use it without getting outofbounds exceptions.

Android SQLite search a column

I have code that gives me a number, and I have an Android SQLite table.
The first column has a lots of numbers and I need to compare the number that I got from the code to these numbers in the table and find the one that matches my number that I got from the code. Once I find it, I need to update the row.....
what is the best way to do it.....?
Your question lacks details, like the name of the column holding the number value and the column(s) being updated when the record is found, but you'll be using something that resembles:
UPDATE your_table
SET column_1 = ?,
column_2 = ?
WHERE number_column = number_from_code

Categories

Resources