Ad

Showing posts with label SQLite. Show all posts
Showing posts with label SQLite. Show all posts

Sunday, July 7, 2013

How to copy sqlite db from Asset Folder in Android ?

 1. Put the dbfile in the asset folder.

2. Create an instance to the dbFile.

File dbfile=new File(parentfolder,dbfile); (we can choose any folder as parent folder)

3. Check whether db is existing or not. if not copy it to the folder.

if (!dbfile.exists()) {
try {
copyDatabase(dbfile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}

private void copyDatabase(File dbFile) throws IOException {
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(dbFile);
 
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
 
os.flush();
os.close();
is.close();
}

Friday, June 28, 2013

Android SQLite Commands with Group By and HAVING

 SELECT * from survey_answer_list_main





SELECT question_id,answer, count(answer)  from survey_answer_list_main where survey_id=1 AND type='single'  group by question_id,answer;

SELECT question_id,answer, count(answer)  from survey_answer_list_main where survey_id=1 AND type='single'  group by question_id,answer having answer LIKE '%yes%'