dimanche 1 novembre 2015

sqlite3 doesn't write web content on my table

I used the following command in order to create my own database:

sqlite3 mydb.db

I used the follwing command for making my table:

CREATE TABLE mytable (hostName CHAR(50) PRIMARY KEY, content TEXT);

And this is part of my script:

hostName='mywebsite.com'
cur=con.cursor()
cur.execute('SELECT hostName FROM mytable WHERE hostName="'+hostName+'"')
data=cur.fetchall()
cont=session.get('http://'+hostName).content
cur.execute('INSERT INTO mytable (hostName,content) VALUES (\''+hostName+'\',\''+str(cont)+'\')')

But when I get the website content it doesn't write to the table when the content is not short.

Maybe there is a parameter larger than TEXT.

How to access SQLite database inside a zip file - android

I have 180 MB SQLite database. If I compress it, it will be just around 40 MB. I keep the file in asset folder and there's no problem in apk size as it is compressed in apk.

But when I copy the db file from the asset folder to phone memory or SD card, then it's 180 MB.

So, is there any way to use compressed db file in android?

Sync SQLite database in real time

I have apps for Android and iOS which store data in local SQLite databases. App works completely offline with no syncing among the devices.

Now, I wish to have two things:

  1. Sync these SQLite databases among various Android and iOS devices. Changes on one device shall be reflected in real time on another device.
  2. Have a web based version of my app where the user can login and access his data. The changes to this data shall then be reflected in real time on his mobile devices as well.

So, a sync required among web based data and mobile SQLite database as well.

I think I need to setup a MySQL (or any other) database (server-side) which shall sync in real time with SQLite database (on mobiles)

I went through Parse, Firebase but they seem to be JSON based (NoSQL). Then I saw AWS and got confused.

Anyone can guide me in the right direction? Which service would solve my problem? Which one would be efficient and easy to implement?

two data in a single column seperated by comma for using sqlite query

please view the days header(sun,mon)

i have two item in days column. For getting the subject according to the day, i have to fire query by comparing the the individual day but there are two or more days in a single column. how could this be done in #Android

Reading Sqlite Cursor carsh on Android 4.2 API 17

I have a table with 145 rows. when i try to get all of it's data it getting crashed on android 4.2, BUT it works fine on android 4.4 emulator.

public ArrayList<QuestionInfoHolder> getAllArticle()
    {
        ArrayList<QuestionInfoHolder> result = new ArrayList<QuestionInfoHolder>();
        SQLiteDatabase db = getReadableDatabase();
        String query = "SELECT * FROM " + ARTICLE_TABLE_NAME + " ORDER BY "+ QUESTION_DATE +";";
        Cursor cursor = db.rawQuery(query, null);
        if(cursor != null)
        {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            {
                result.add(new QuestionInfoHolder(cursor.getInt(0),
                        cursor.getInt(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getString(4)));
            }
        }

        return result;
    }

the logcat:

Failed to read row 9, column 0 from a CursorWindow which has 9 rows, 5 columns.
FATAL EXCEPTION: main
java.lang.IllegalStateException: Couldn't read row 9, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

this says the cursor has only 9 rows, but if i log cursor.getCount(); it will show 145.

i can not figure out what is the problem. so any help or suggestion will be appreciated.

Issue with Activity Context and SQLite

I'm getting a nullPointerException in my code when i try pull all the columns out of my (small) database. From what I can tell, it's caused by me not having the right Activity context when creating my database. It's great knowing what i'm doing wrong, but i can't see how to fix it! can any of you point me in the right direction?

Here is my method for pulling the data out.

    public ArrayList<Task> getAllTasks() throws ParseException {
    ArrayList<Task>  tempArraList = null;
    Task tempTask = null;

    String[] cols = {TaskDBContract.TaskEntry.COLUMN_NAME_TASK_ID,
            TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION,
            TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME,
            TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY
    };
    String selection = "*";
    String[] selectionArgs = null;
    String groupBy = null;
    String having = null;
    String orderBy = null;



    Cursor c = database.query(
            TaskDBContract.TaskEntry.TABLE_NAME,
            cols,
            selection,
            selectionArgs,
            groupBy,
            having,
            orderBy
    );
    int index = 1;
    while (c.moveToNext()){

        if (index == 0){ tempTask.location = c.getInt(c.getPosition());}
        if (index == 1){ tempTask.task = c.getString(c.getPosition());}
        if (index == 2){ tempTask.date.setTime(sdf.parse(c.getString(c.getPosition())));}
        if (index == 3){
            tempTask.category = c.getString(c.getPosition());
            tempArraList.add(tempTask);
        }

        index ++;
        if (index == 4){index = 0;};

    }
    c.close();

    return tempArraList;
}

Here is the header on my Database class

public class TaskDatabase {
public static final String DATABASE_NAME = "Taskdb";
private static final String COMMA_SEP = ",";
private static final String TAG = "DatabaseConnector";
private SQLiteDatabase database;
private taskDBHelper dbOpenHelper;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d kk:mm", Locale.getDefault());

public TaskDatabase(Context context) {
    dbOpenHelper = new taskDBHelper(context, DATABASE_NAME);
}

public void openDatabaseConnection() throws SQLException
{
    //open database in reading/writing mode
    database = dbOpenHelper.getWritableDatabase();
}

public void closeDatabaseConnection() throws SQLException
{
    if (database != null)
        database.close();
}

and Here is my Helper class.

public class taskDBHelper extends SQLiteOpenHelper {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d kk:mm", Locale.getDefault());
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + TaskDBContract.TaskEntry.TABLE_NAME + " (" +
                TaskDBContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION + TEXT_TYPE + COMMA_SEP +
                TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME + TEXT_TYPE + COMMA_SEP +
                TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY + TEXT_TYPE + COMMA_SEP + " )";

private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + TaskDBContract.TaskEntry.TABLE_NAME;
// If you change the database schema, you must increment the database version.


public static final int DATABASE_VERSION = 1;


public taskDBHelper(Context context, String name) {
    super(context, name, null, DATABASE_VERSION);
}

public taskDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
    super(context, name, factory, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_ENTRIES);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // This database is only a cache for online data, so its upgrade policy is
    // to simply to discard the data and start over
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
}

public String insertTask(Task temptask) {
    sdf.setCalendar(temptask.date);


    String SQL_TASK_INSERT = "INSERT INTO " + TaskDBContract.TaskEntry.TABLE_NAME +
            "(" + TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION + COMMA_SEP +
            TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME + COMMA_SEP +
            TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY + COMMA_SEP + " ) VALUES (" +
            temptask.task + COMMA_SEP + sdf.format(temptask.date) + COMMA_SEP +
            temptask.category + ")";

    return SQL_TASK_INSERT;
}

public void InsertTaskArray(ArrayList<Task> taskArrayList) {


    for (int j = 0; j < taskArrayList.size(); j++) {
        sdf.setCalendar(taskArrayList.get(j).date);

        String SQL_TASK_INSERT = "INSERT INTO " + TaskDBContract.TaskEntry.TABLE_NAME +
                "(" + TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DESCRIPTION + COMMA_SEP +
                TaskDBContract.TaskEntry.COLUMN_NAME_TASK_DATETIME + COMMA_SEP +
                TaskDBContract.TaskEntry.COLUMN_NAME_TASK_CATEGORY + COMMA_SEP + " ) VALUES (" +
                taskArrayList.get(j).task + COMMA_SEP + sdf.format(taskArrayList.get(j).date) + COMMA_SEP +
                taskArrayList.get(j).category + ")";
    }

}

}

and the crash log (added)

11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: FATAL EXCEPTION: main
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: Process: com.example.balefire.test, PID: 9032
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{http://ift.tt/1WqIDmc}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at com.example.balefire.test.TaskDatabase.getAllTasks(TaskDatabase.java:138)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at com.example.balefire.test.MainActivity.onCreate(MainActivity.java:72)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-01 01:51:29.144 9032-9032/com.example.balefire.test E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)      

I know i'm doing something stupid and just can't see it!!

samedi 31 octobre 2015

How to Execute UpdateAsync with Where statement

I have a table with 1 row. log => 1 | flagLog => 0

 public async void updateFlag()
    {
        var local = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "hinos.sqlite");
        SQLiteAsyncConnection con = new SQLiteAsyncConnection(local, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite);

        var updateFlagLog = await con.FindAsync<logon>(u => u.log == 1);
        if (updateFlagLog != null)
        {
             await con.UpdateAsync(?????);
        // update logon set flagLog = 1 where log = 1;

        }
    }

I got the row with this comand

var updateFlagLog = await con.FindAsync<logon>(u => u.log == 1);

But i dont know how i use to update this row.