how I can Use two table in a time with Xamarin Android - android

I have one connection for each table in a database like the below, How Can I use Two Table in a time?? My Main trouble is how connect to Two Table in a time to use them. Please somebody tell me about that
part of My code is:
public bool createDataBase()
{
try
{
using (var connection = new
SQLiteConnection(System.IO.Path.Combine(folder, "Plans.db")))
{
connection.CreateTable<Plan>();
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public bool createDataBase2()
{
try
{
using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "PlanDoned.db")))
{
connection.CreateTable<Plan>();
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}
public bool insertIntoTablePlanDoned(Plan plan)
{
try
{
using (var connection = new
SQLiteConnection(System.IO.Path.Combine(folder, "PlanDoned.db")))
{
//connection.Query<Plan>("UPDATE Plan SET PlanName=?,PlanDate=?,PlanDesc=? where Id=?", plan.PlanName, plan.PlanDate, plan.PlanDesc, plan.Id);
//return true;
connection.Query<Plan>("insert into PlanDoned select * from Plans where Id=?", plan.Id);
return true;
}
}
catch (SQLiteException ex)
{
Log.Info("SQLiteEx", ex.Message);
return false;
}
}

use ATTACH DATABASE 'Database2Path' As 'Database2Name';
Then use both Database1 and Database2 in the same query i.e.:
INSERT INTO Database2Name.Table SELECT Column1, Column2 FROM Database1.Table

Related

How to sql query only once insert on android

I want A table only once insert sql query.
before insert query. if my A table have 1 record. execute update 1 record.
and my A table have 0 record. insert query.
I know only use insert query.
public class compareInsert extends Thread {
private Connection mConnection = null;
private Statement mStatement = null;
#Override
public void run() {
super.run();
Looper.prepare();
setDB();
Looper.loop();
}
public void setDB() {
mConnection = DBconnect.connectJDBC(); // DB Connect.
try {
mStatement = mConnection.createStatement();
mStatement.executeQuery("use be");
String insertq = "insert into A"
+ "(name, old, num)"
+ "values"
+ "(?, ?, ?);
PreparedStatement statement = mConnect.prepareStatement(insertq);
statement.setString(1, "hyunwook");
statement.setString(2, "17");
statement.setString(3, "2732");
int ret = statement.executeUpdate();
if (ret > 0) {
Log.d(TAG, "SUCCESS");
} else {
Log.d(TAG, "failed");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
mConnection.close();
} catch (Exception e) {
}
}
}
}
this source is insert query code.
but app start, always insert query.
please help for me
thanks.

Azure mobile service for android query table

I'm using azure sdk for android and follow the tutorial https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-android-get-started-data/.
When I'm trying to connect and insert data to mobile service table all is ok, but when I query the table in activity my app gets stuck, though there are only several entries in the table and execute method successfully returns Future.
public static MobileServiceClient mClient;
public static void connect(Context context) {
try {
mClient = new MobileServiceClient(storageLink, key, context);
} catch (MalformedURLException e) {
Log.e("AzureService.connect", "Storage access failed" + storageLink);
}
}
public static InstallationData get(final String deviceId) {
MobileServiceTable<InstallationData> table= mClient.getTable(InstallationData.class);
final MobileServiceList<InstallationData> result;
try {
result = table.where().field("deviceid").eq(deviceId).execute().get();
for (InstallationData item : result) {
return item;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
public static void store(final InstallationData item) {
mClient.getTable(InstallationData.class).insert(item, new TableOperationCallback<InstallationData>() {
public void onCompleted(InstallationData entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
Log.d("AzureService.store()", "Data about " + item.getDeviceid() + "" + "is successfully updated");
} else {
exception.printStackTrace();
Log.e("AzureService.store()", "Data about " + item.getDeviceid() + "" + "is failed to update");
}
}
});
}
Thank you in advance!

ORMLite not update with the same count of object

I try to make simple test. I have json response from REST API with ~250 objects shops and the same response in file but first shop has different name. First step is load JSON file with ~250 shops and save in local db using ORMLite. Next step is connect with API and get the same response. New json response is parse and UPDATE exist elements in database. What is result?
Now I go to simple Activity with ListView and call getShopsAll(). This method return old data before update. Why?
This is method to save shop in database:
public void createShop(final ArrayList<ModelSklep> list) throws Exception {
getDaoShop().callBatchTasks(new Callable<Void>() {
#Override
public Void call() {
for(ModelSklep item : list) {
createShop(item);
if(item.getHoursList().size() > 0) {
createHours(item.getHoursList());
}
}
return null;
}
});
}
Method createshop:
public boolean createShop(ModelSklep item) {
try {
getDaoShop().createOrUpdate(item);
return true;
} catch (SQLException e) {
return false;
}
}
Method getShopAll:
public ArrayList<ModelSklep> getShopAll() {
try {
return (ArrayList<ModelSklep>) getDaoShop().queryBuilder().orderByRaw(
"nazwaPelna COLLATE LOCALIZED ASC").query();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
Object is updating. But getShopAll() return old data object.
Clear cache after update doesn't work.
Help. I don't know how resolve this problem.
[Edit]
public Dao<ModelSklep, Integer> getDaoShop() {
if(modelShopDao == null) {
try {
modelShopDao = getDao(ModelSklep.class);
modelShopDao.setObjectCache(true);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return modelShopDao;
}

how to implement common method for select data from sqllite db in android

I try to implement sample apk for KIOSK .I have many tables. So I would like to implement common methods for all database tasks(select , insert , update , delete ). Now I create select method in one class like this..
public List<OrgcodeInfo> OrgList(String sql) {
cursor = null;
try {
cursor = mDb.rawQuery(sql, null);
_listOrgcodeInfo = new ArrayList<OrgcodeInfo>();
while (cursor.moveToNext()) {
_OrgcodeInfo = new OrgcodeInfo();
_OrgcodeInfo.setOrgcode(cursor.getString(cursor
.getColumnIndex("Org Code")));
_OrgcodeInfo.setOrgName(cursor.getString(cursor
.getColumnIndex("Shop Name")));
_listOrgcodeInfo.add(_OrgcodeInfo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
mDb.close();
}
return _listOrgcodeInfo;
}
This is OK for small tasks.But I have many tasks for data transaction. I create object for every table as OrgcodeInfo and set data every time.
How can I do it.
public class DBconnection {
SQLiteDatabase database;
public DBconnection(Context context){
try {
// TODO Auto-generated constructor stub
database=context.openOrCreateDatabase("hslogin",SQLiteDatabase.OPEN_READWRITE,null);
database.execSQL("create table if not exists login(username varchar(40) PRIMARY KEY,password varchar(40))");
database.execSQL("create table if not exists instructions(sourcedestination varchar(100) ,instruction varchar(80),lattitude varchar(100),longitude varchar(100))");
} catch (Exception e) {
// TODO: handle exception
}
}
public int putData(String sql) {
int i;
try {
database.execSQL(sql);
i=1;
} catch (Exception e) {
i=0;
// TODO: handle exception
}
return i;
}
public Cursor getData(String sql) {
Cursor cursor=null;
try {
cursor=database.rawQuery(sql, null);
} catch (Exception e) {
// TODO: handle exception
}
return cursor;
}}

Need Wrapper for API16 method in SQLite

With API16 the new WAL (Write Ahead Logging) was introduced in Androids SQLiteDatabase class. I would like to test if WAL is enabled for a SQLite database. The app runs on older Android releases too, so I need a wrapper class for these new functions in SQLiteDatabase. The functions are:
public boolean isWriteAheadLoggingEnabled()
public boolean enableWriteAheadLogging()
public void disableWriteAheadLogging ()
In the Android Developer Blog I did find an article for a wrapper class that wraps new classes. What I didn't find is a wrapper for new methods in an already existing class. How should I do that?
The constructor for SQLiteDatabase is private so your're not going to be able to extend it and add "wrappers" to the class itself. You can however just write a "helper" wrapper like so:
public class WALWrapper {
private boolean mAvailable;
private Method mIsWriteAheadLoggingEnabled;
private Method mEnableWriteAheadLogging;
private Method mDisableWriteAheadLogging;
private final SQLiteDatabase mDb;
public WALWrapper(SQLiteDatabase db) {
mDb = db;
mAvailable = false;
try {
mIsWriteAheadLoggingEnabled =
SQLiteDatabase.class.getMethod("isWriteAheadLoggingEnabled");
mEnableWriteAheadLogging =
SQLiteDatabase.class.getMethod("enableWriteAheadLogging");
mDisableWriteAheadLogging =
SQLiteDatabase.class.getMethod("disableWriteAheadLogging");
mAvailable = true;
} catch (NoSuchMethodException e) {
}
}
/**
* Returns <code>true</code> if the {#link #isWriteAheadLoggingEnabled()},
* {#link #enableWriteAheadLogging()} and {#link #disableWriteAheadLogging()}
* are available.
* #return <code>true</code> if the WALWrapper is functional, <code>false</code>
* otherwise.
*/
public boolean isWALAvailable() {
return mAvailable;
}
public boolean isWriteAheadLoggingEnabled() {
boolean result = false;
if (mIsWriteAheadLoggingEnabled != null) {
try {
result = (Boolean) mIsWriteAheadLoggingEnabled.invoke(mDb);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
public boolean enableWriteAheadLogging() {
boolean result = false;
if (mEnableWriteAheadLogging != null) {
try {
result = (Boolean) mEnableWriteAheadLogging.invoke(mDb);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
return result;
}
public void disableWriteAheadLogging() {
if (mDisableWriteAheadLogging != null) {
try {
mDisableWriteAheadLogging.invoke(mDb);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
}

Categories

Resources