Android, How to Exec a SQL File in Sqlitedatabase

android, how to exec a sql file in sqlitedatabase

I wrote this one especially for you <3

I used the same filename as you "/raw/food_db.sql" but that lead to Errors instead I had to call it "/raw/food_db". I guess its because you don't use filenames in your code, but ResourceIds which are written like "R.raw.food_db" and the dot is confusing the system.

There is a method for within your DbSource... assuming somewhere there is code like this:

private SQLiteDatabase db;
...
DbHelper dbHelper = new DbHelper(context);
this.db = dbHelper.getWritableDatabase();

You put this method in there:

/**
* This reads a file from the given Resource-Id and calls every line of it as a SQL-Statement
*
* @param context
*
* @param resourceId
* e.g. R.raw.food_db
*
* @return Number of SQL-Statements run
* @throws IOException
*/
public int insertFromFile(Context context, int resourceId) throws IOException {
// Reseting Counter
int result = 0;

// Open the resource
InputStream insertsStream = context.getResources().openRawResource(resourceId);
BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));

// Iterate through lines (assuming each insert has its own line and theres no other stuff)
while (insertReader.ready()) {
String insertStmt = insertReader.readLine();
db.execSQL(insertStmt);
result++;
}
insertReader.close();

// returning number of inserted rows
return result;
}

Call it like this (I tried from an Activity, so that Toasts can output messages). Look closely, the errors are "Toasted" as well.

try {
int insertCount = database.insertFromFile(this, R.raw.food_db);
Toast.makeText(this, "Rows loaded from file= " + insertCount, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}

Enjoy!

Oh.. btw: This code is meant for a file in which each insert-Statement has its own line.

How can I execute a *.sql script on android programmatically?

InputStream is = getResources().getAssets().open("SQLScript.sql");
String sql= convertStreamToString(is);

public static String convertStreamToString(InputStream is)
throws IOException {
Writer writer = new StringWriter();
char[] buffer = new char[2048];
try {
Reader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String text = writer.toString();
return text;
}

SQLiteDatabase db;
db = openOrCreateDatabase("MyDatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY, null );
db.execSQL(sql);

How to execute .sql file into android?

I saw that i can execute a .sql file with shell commands

That would be a really bad idea. There are no shell commands that are part of the Android SDK and therefore are guaranteed to be on all devices.

Read in the SQL script and execute its statements via execSQL() and kin on SQLiteDatabase, perhaps wrapping them in your own transaction using beginTransaction() and kin.

Sqlite schema and sqlite data import from external .sql file in Android

If I understand your question the following may be along the lines of what you are looking for :-

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("mydb"),null);
// Loop through schema.sql (Noting that execSQL will only execute 1 SQL statment at a time)
db.execSQL("CREATE TABLE IF NOT EXISTS tablea (_name TEXT, _anotherrow TEXT, _etc TEXT)");
if (DatabaseUtils.queryNumEntries(db,"tablea") < 1) {
// Loop through seeddata here (if actual SQL then use execSQL)
ContentValues cv = new ContentValues();
cv.put("_name", "This is the name.");
cv.put("_anotherrow", "Someother data");
cv.put("_etc", "and on and on.....");
db.insert("tablea", null, cv);
}
Cursor csr = db.query("tablea",null,null,null,null,null,null);
while (csr.moveToNext()) {
Log.d("TABLEINFO","Row " + csr.getPosition());
for(String s: csr.getColumnNames()) {
Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
}
}

This will

  • create the database named mydb, if it doesn't exist and then open the database.
  • try to create the table named tablea if it doesn't exist.
  • check how many rows are in the table and if the number is less than 1 then

    • insert a row
  • extract the data into the Cursor name csr
  • For each row in the table it will

    • Write a line to the log detailing the row number (0 as first) then for each column
    • Write a line to the log detailing the column name and the data from that column for the row.

e.g.

11-24 17:06:04.601 3360-3360/? D/TABLEINFO: Row 0
11-24 17:06:04.601 3360-3360/? D/TABLEINFO: Column=_name Data=This is the name.
11-24 17:06:04.601 3360-3360/? D/TABLEINFO: Column=_anotherrow Data=Someother data
11-24 17:06:04.601 3360-3360/? D/TABLEINFO: Column=_etc Data=and on and on.....

Complete working solution

  • 1 File schema.sql, which has been placed into the assets folder (in this case for a project named ImportSchemaAndData D:\Android_Applications\ImportSchemaAndData\app\src\main\assets\schema.sql)

:-

CREATE TABLE tablea (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
CREATE TABLE tableb (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
CREATE TABLE tablec (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
  • 2 File seeddata.sql (likewise placed into assets folder)

:-

INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")

Note you may have to create the assets folder (see above for location)

  • 3 The Code (in this case in MainActivity)

:-

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InputStream schema, seeddata;
String lineofdata;
SQLiteDatabase db;
Cursor csr;

try {
schema = this.getAssets().open("schema.sql");
seeddata = this.getAssets().open("seeddata.sql");
} catch (IOException ioe) {
ioe.printStackTrace();
return; //???? used return just for brevity
}
File dbpath = new File(this.getDatabasePath("mydb").getParent());
if (!dbpath.exists()) {
dbpath.mkdirs();

db = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("mydb"), null);
BufferedReader br = new BufferedReader(new InputStreamReader(schema));
try {
while ((lineofdata = br.readLine()) != null) {
Log.d("ACTIONSQL", "Actioning " + lineofdata);
db.execSQL(lineofdata);
}
schema.close();
} catch (IOException ioe) {
ioe.printStackTrace();
try {
schema.close();
} catch (IOException ioe2) {
ioe2.printStackTrace();
}
}
db.close();
db = SQLiteDatabase.openDatabase(this.getDatabasePath("mydb").getPath(),null, Context.MODE_PRIVATE);
lineofdata = "";
br = new BufferedReader(new InputStreamReader(seeddata));
try {
while ((lineofdata = br.readLine()) != null) {
Log.d("ACTIONSQL", "Actioning " + lineofdata);
db.execSQL(lineofdata);
}
} catch (IOException ioe) {
ioe.printStackTrace();
db.endTransaction();
}
} else {
db = SQLiteDatabase.openDatabase(this.getDatabasePath("mydb").getPath(),null,Context.MODE_PRIVATE);
}

csr = db.query("tablea",null,null,null,null,null,null);
while (csr.moveToNext()) {
Log.d("TABLEINFO","Row " + csr.getPosition());
for(String s: csr.getColumnNames()) {
Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
}
}
csr = db.query("tableb",null,null,null,null,null,null);
while (csr.moveToNext()) {
Log.d("TABLEINFO","Row " + csr.getPosition());
for(String s: csr.getColumnNames()) {
Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
}
}
csr = db.query("tablec",null,null,null,null,null,null);
while (csr.moveToNext()) {
Log.d("TABLEINFO","Row " + csr.getPosition());
for(String s: csr.getColumnNames()) {
Log.d("TABLEINFO","\tColumn=" + s + " Data=" + csr.getString(csr.getColumnIndex(s)));
}
}
}
}

Result for initial run :-

11-24 19:20:02.182 4652-4652/? D/ACTIONSQL: Actioning CREATE TABLE tablea (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
11-24 19:20:02.187 4652-4652/? D/ACTIONSQL: Actioning CREATE TABLE tableb (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
11-24 19:20:02.191 4652-4652/? D/ACTIONSQL: Actioning CREATE TABLE tablec (_id INTEGER PRIMARY KEY, _name TEXT, _otherdata TEXT, _etc)
11-24 19:20:02.197 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
11-24 19:20:02.201 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
11-24 19:20:02.204 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
11-24 19:20:02.206 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablea (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
11-24 19:20:02.209 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
11-24 19:20:02.213 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
11-24 19:20:02.216 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
11-24 19:20:02.219 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tableb (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
11-24 19:20:02.222 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Fred","Data for Fred","even more data for Fred.")
11-24 19:20:02.224 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Bert","Data for Bert","even more data for Bert.")
11-24 19:20:02.227 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Tom","Data for Tom","even more data for Tom.")
11-24 19:20:02.231 4652-4652/? D/ACTIONSQL: Actioning INSERT INTO tablec (_name,_otherdata, _etc) VALUES("Harry","Harry has this data.","Harry has even more data.")
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 0
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Fred.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Bert.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Tom.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=4
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Harry
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Harry has this data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=Harry has even more data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 0
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Fred
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Fred.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 1
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Bert
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Bert.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 2
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Tom
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Tom.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 3
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_id Data=4
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_name Data=Harry
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Harry has this data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Column=_etc Data=Harry has even more data.
11-24 19:20:02.235 4652-4652/? D/TABLEINFO: Row 0
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_id Data=1
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_name Data=Fred
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Fred
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Fred.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Row 1
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_id Data=2
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_name Data=Bert
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Bert
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Bert.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Row 2
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_id Data=3
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_name Data=Tom
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Data for Tom
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_etc Data=even more data for Tom.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Row 3
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_id Data=4
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_name Data=Harry
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_otherdata Data=Harry has this data.
11-24 19:20:02.236 4652-4652/? D/TABLEINFO: Column=_etc Data=Harry has even more data.

Result for subsequent run :-

11-24 19:20:20.605 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 0
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Fred.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Bert.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Tom.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=4
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Harry
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Harry has this data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=Harry has even more data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 0
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Fred.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Bert
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Bert.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 2
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Tom
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Tom.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 3
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=4
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Harry
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Harry has this data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=Harry has even more data.
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 0
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=1
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Fred
11-24 19:20:20.606 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Fred.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 1
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=2
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Bert
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Bert
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Bert.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 2
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=3
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Tom
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Data for Tom
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=even more data for Tom.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Row 3
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_id Data=4
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_name Data=Harry
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_otherdata Data=Harry has this data.
11-24 19:20:20.607 4709-4709/mjt.importschemaanddata D/TABLEINFO: Column=_etc Data=Harry has even more data.

How to run query in SQLite database in android?

final Cursor cursor = db.rawQuery("SELECT SUM(odometer) as odometer FROM tripmileagetable where date like '2012-07%';", null);
int sum = 0;
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
sum = cursor.getInt(0);
}
} finally {
cursor.close();
}
}

How to create a SQL database for android?

You just have to add a sqlite file with the database to the app resources if i remember right, and use it with a DBHelper class like these but adapted to your app needs.

I personally create and edit the sqlite files with this Firefox extension.

Executing multiple statements with SQLiteDatabase.execSQL

That's not possible to do using the standard methods which comes with Android. So, if you want to execute batch of multiple SQL statements, you will have to create your own utility to do so. For instance, you can have something like this:

public void executeBatchSql(String sql){
// use something like StringTokenizer to separate sql statements
for each sql statement{
database.execSQL(oneStatement);
}
}

Though, what I'd do is something like this:

String sql1 = "create bla bla bla;";
String sql2 = "create foo bar;";
String[] statements = new String[]{sql1, sql2};

// then
for(String sql : statements){
database.execSQL(sql);
}


Related Topics



Leave a reply



Submit