Performing INSERT operation in Android SQLite with execSQL

Inquiry:
The given code is for an insert or replace query. I want to insert an image in byte array format into this table as an additional field. The data type for the image is BLOB in the table. However, I cannot pass the byte array format as a parameter into the above sqlite.execsql func. How can I insert the image in its original format without converting it to a string?
Solution 1:
Instead of the insert method, use a variant of the insert method that takes a ContentValues object, which enables you to use the byte array directly.
Solution 2:
You can use this method:
Solution 1:
According to the Android SQLiteDatabase class documentation:
Later on:
As far as I can tell, the
method is more for higher level database operations, such as creating tables and changing schema, and the
,
,
, etc. methods should be used to modify rows.

Question:

Based on the available documentation and the information provided in this post, I am aware that

execSQL()

performs a specific
SQL statement
operation, which is not related to

SELECT

or any other SQL command that
returns data
. Additionally, it has been advised that

execSQL()

must not be utilized for insertion purposes.

I utilize

execSQL()

to perform insertions, such as:

db.execSQL("INSERT INTO LIST VALUES('খবর');"); //where LIST is a table

Although it is functioning flawlessly for me and there is no instance of

SELECT

statement in conjunction with

INSERT

, I have been cautioned against using it in this context.

I am wondering if the

INSERT

statement would generate a value in the absence of an attached

SELECT

statement.


Solution 1:

Although it is possible to use it, it is not advisable as you will not be able to obtain any data that may be generated by the DB. The suggested methods will provide you with return codes, cursors, or other information to inform you of what occurred, allowing you to address any problems that may arise.

The crucial element can be found in the documentation of

execSQL

.

It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert...

The crucial term to note is “encouraged.” While you have the ability to utilize

execSQL

for any SQL command, the lack of feedback during the process renders it risky to employ for tasks requiring feedback. This is unless you don’t mind disregarding failed inserts or updates that only affect a portion of the records.


Solution 2:



INSERT

In Sqlite, result sets are not returned by statements.

According to the Android documentation, the following information pertains to

execSQL

.

Perform a
single SQL
operation that does not involve executing a SELECT or any other SQL statement that retrieves information. Such operations do not provide any means to return data, including the number of affected rows. As an alternative, it is recommended that you utilize functions such as
insert(String, String
,
contentvalues
, update(String, ContentValues, String, String[]), and others whenever possible.

Although not an Android programmer, I wouldn’t recommend using the

insert

function over

execSQL

unless the goal is to retrieve the

rowid

of the inserted record. Moreover, the explanation provided is unclear as it suggests that inserting data could result in some data being returned.

Keep in mind that

insert

may have certain benefits that make it preferable over

execSQL

. Additionally, it’s important to consider “best practices” when choosing to use

insert

.


Solution 3:


The process of performing an insertion can be carried out using the method described below.

 SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    //2nd argument is String containing nullColumnHack
    db.close(); // Closing database connection

To acquire additional insights, you can visit the website specified by the following code: http://www.blazin.in/2016/02/understanding-sqlite-database-in-android.html.

Frequently Asked Questions