Skip to content

Instantly share code, notes, and snippets.

@AungWinnHtut
Created August 15, 2016 15:04
Show Gist options
  • Save AungWinnHtut/f24841a64babe06fa69f270116ba5352 to your computer and use it in GitHub Desktop.
Save AungWinnHtut/f24841a64babe06fa69f270116ba5352 to your computer and use it in GitHub Desktop.
package com.engineer4myanmar.intakebook;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* @author manish.s
* Updat by Dr. Aung Win Htut
*/
public class SQLiteConnector {
public String selectQueryb1 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၁)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb2 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၂)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb3 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၃)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb4 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၄)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb5 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၅)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb6 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၆)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb7 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၇)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQueryb8 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၈)တပ္ခြဲ' and bat='ဘုရင္႔ေနာင္တပ္ရင္း'";
public String selectQuerya1 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၁)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya2 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၂)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya3 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၃)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya4 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၄)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya5 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၅)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya6 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၆)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya7 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၇)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
public String selectQuerya8 = "SELECT * FROM " + TABLE_RECORD+" where com='အမွတ္(၈)တပ္ခြဲ' and bat='အေနာ္ရထာတပ္ရင္း'";
private SQLiteDatabase database;
private SQLiteHelper sqlHelper;
private Cursor cursor;
//You can change database *** table name *** here
private static final String TABLE_RECORD = "phonetb";
public SQLiteConnector(Context context) {
sqlHelper = new SQLiteHelper(context);
}
// Getting All records from *** k columns *** zero base
public List<String> getAllRecord(int iColumns) {
List<String> studentList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_RECORD;
database = sqlHelper.getReadableDatabase();
cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
studentList.add(cursor.getString(iColumns));
} while (cursor.moveToNext());
}
database.close();
return studentList;
}
//Getting specific records from *** k columns *** zero base depends on ***selectQuery***
public List<String> getGroupRecord(int iColumns,String selectQuery) {
System.out.println("getGroupRecord is called by i= "+iColumns);
List<String> studentList = new ArrayList<String>();
database = sqlHelper.getReadableDatabase();
cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
studentList.add(cursor.getString(iColumns));
} while (cursor.moveToNext());
}
database.close();
System.out.println(studentList.toString());
return studentList;
}
}
package com.engineer4myanmar.intakebook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
/**
* @author manish.s
* Update by Dr. Aung Win Htut
*/
public class SQLiteHelper extends SQLiteOpenHelper {
private static String DB_PATH="/data/data/com.engineer4myanmar.intakebook/databases/";
private static String DB_NAME = "third.db3";//"mydatabase.sqlite";
private static int VERSION =1;
private SQLiteDatabase myDataBase;
private final Context myContext;
public SQLiteHelper(Context context) {
super(context, DB_NAME, null, VERSION);
myContext = context;
try {
createDatabase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
}
public void createDatabase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
System.out.println("DB EXIST");
//later need to delete this codes
this.getReadableDatabase();
this.close();
copyDataBase();
}
else {
this.getReadableDatabase();
this.close();
copyDataBase();
}
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
System.out.println("Database does't exist yet.");
System.out.println(e.toString());
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment