SQLite Android Bindings

Check-in [69b389af43]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Start setting up some infrastructure code for a test suite. Add a test demonstrating the problem with type Cursor.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 69b389af438de5f185868eb3e2a4b91c9076152d
User & Date: dan 2013-12-20 17:02:04.967
Context
2013-12-21
16:04
Replace nativeExecuteForCursorWindow() with an implementation that builds with the NDK. Seems to work, but is not yet tested. Exception handling is almost certainly still wrong. (check-in: 365586dcaf user: dan tags: trunk)
2013-12-20
17:02
Start setting up some infrastructure code for a test suite. Add a test demonstrating the problem with type Cursor. (check-in: 69b389af43 user: dan tags: trunk)
2013-12-19
18:42
Fix logging macros. (check-in: 3f19250fc5 user: dan tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to res/layout/main.xml.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25








26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, CustomSqlite"
    />
<TextView
    android:id="@+id/tv_widget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="this text should be replaced by the sqlite version"
    />

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find SQLite version"
        android:onClick="find_version"
        />








</LinearLayout>











|
<
<
|
<
<
<





|
|

>
>
>
>
>
>
>
>


1
2
3
4
5
6
7
8
9
10
11


12



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="CustomSqlite Tests"


    android:typeface="monospace"



    />

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Run the tests"
        android:onClick="run_the_tests"
        />

<TextView
    android:id="@+id/tv_widget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="&lt;this text should be replaced by the test output&gt;"
    android:typeface="monospace"
    />
</LinearLayout>

Changes to src/org/sqlite/app/customsqlite/CustomSqlite.java.
1
2
3
4
5
6
7
8
9
10
11
12


13
14
15
16
17
18
19




20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
35



36


37















38
39


40

41


42






43

44

45
46


47
48


49
50


51



52
53
54



package org.sqlite.app.customsqlite;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.sqlite.database.sqlite.SQLiteDatabase;
import org.sqlite.database.sqlite.SQLiteStatement;



/*
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
*/

public class CustomSqlite extends Activity
{




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public void find_version(View view){

      System.loadLibrary("sqliteX");

      SQLiteDatabase db = null;
      SQLiteStatement st;
      String res;




      TextView tv = (TextView)findViewById(R.id.tv_widget);


      tv.setText("<attempting to access sqlite...>");
















      try {


        db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);

        st = db.compileStatement("SELECT sqlite_version()");


        res = st.simpleQueryForString();








        tv.setText(res);

            
      }catch(Exception e){


        Log.e("Error", "Error", e);
        tv.setText(e.toString());


      } finally {
        if (db != null)


          db.close();



      }
    }
}














>
>







>
>
>
>
|
|
|
<
|
|
>
|

|
<
<
<
|
|
|

>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
|
>
|
>
>
|
>
>
>
>
>
>
|
>
|
>
|
|
>
>
|
|
>
>
|
|
>
>
|
>
>
>
|
|

>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

29
30
31
32
33
34



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

package org.sqlite.app.customsqlite;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.sqlite.database.sqlite.SQLiteDatabase;
import org.sqlite.database.sqlite.SQLiteStatement;

import android.database.Cursor;

/*
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
*/

public class CustomSqlite extends Activity
{
  private TextView myTV;          /* Text view widget */
  private int myNTest;            /* Number of tests attempted */
  private int myNErr;             /* Number of tests failed */

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myTV = (TextView)findViewById(R.id.tv_widget);
  }

  public void report_version(){



    SQLiteDatabase db = null;
    SQLiteStatement st;
    String res;

    db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);
    st = db.compileStatement("SELECT sqlite_version()");
    res = st.simpleQueryForString();

    myTV.append("SQLite version " + res + "\n\n");
  }

  public void test_result(String name, String res, String expected){
    myTV.append(name + "... ");
    myNTest++;

    if( res.equals(expected) ){
      myTV.append("ok\n");
    } else {
      myNErr++;
      myTV.append("FAILED\n");
      myTV.append("   res=     \"" + res + "\"\n");
      myTV.append("   expected=\"" + expected + "\"\n");
    }
  }

  /*
  ** Use a Cursor to loop through the results of a SELECT query.

  */
  public void csr_test_1(){
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(":memory:", null);
    String res = "";

    db.execSQL("CREATE TABLE t1(x)");
    db.execSQL("INSERT INTO t1 VALUES ('one'), ('two'), ('three')");
    
    Cursor c = db.rawQuery("SELECT x FROM t1", null);
    if( c!=null ){
      boolean bRes;
      for(bRes=c.moveToFirst(); bRes; bRes=c.moveToNext()){
        String x = c.getString(0);
        res = res + "." + x;
      }
    }

    test_result("csr_test_1", res, ".one.two.three");
  }

  public void run_the_tests(View view){
    System.loadLibrary("sqliteX");

    myTV.setText("");
    myNErr = 0;
    myNTest = 0;

    try {
      report_version();
      csr_test_1();

      myTV.append("\n" + myNErr + " errors from " + myNTest + " tests\n");
    } catch(Exception e) {
      myTV.append("Exception: " + e.toString());
    }
  }
}