PhoneGap에서 SQLITE를 사용하기 위한 설정이다. 구조는 이미지와 같으며

다음 순서로 기본 세팅을 한다.

1. AndroidManifest.xml
   : 권한 추가. 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

--전체

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.nobang.phonegapsql"

    android:versionCode="1"

    android:versionName="1.0" >


    <uses-sdk

        android:minSdkVersion="16"

        android:targetSdkVersion="19" />


    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.nobang.phonegapsql.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   

</manifest>

2. /res/xml/config.xml
 : 폰갭 라이브러리 세팅을 하면 config.xml을 해당 위치에 복사하게 되는데 sqlite를 사용하려면 이것에 플러그인 설정을 추가해야한다. <plugins></plugins> 사이에 네가지를 추가한다.

<?xml version="1.0" encoding="UTF-8"?>

<widget xmlns     = "http://www.w3.org/ns/widgets"

        id        = "io.cordova.helloCordova"

        version   = "2.0.0">

<plugins>

        <plugin name="SQLitePlugin" value="org.pgsqlite.SQLitePlugin"/>

      <plugin name="App" value="org.apache.cordova.App"/>

      <plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>

      <plugin name="Device" value="org.apache.cordova.Device"/>

    </plugins>

</widget>

3. /src/org.pgsqlite/SQLitePlugin.java
   해당 파일을 그래도 사용한다. 내 프로젝트에서 사용하기때문에 패키지명을 바꿨더니 javascript에서 에러남.

4. /assets/www/js 아래에 cordova.js 와 SQLitePlugin.js 를 넣는다

5. index.html을 작성한다

  cordova.js와 SQLitePlugin.js를 import하고 
  문서가 onLoad될 때, db 및 table 생성, 조회를 한문장으로 처리

(필요시 각각 기능을 나누면 됨 )


<!DOCTYPE html>

<html>

  <head>

  <title>Lawnchair Spec</title>

  

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />

<meta charset="utf-8">


<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>

    <script type="text/javascript" charset="utf-8" src="js/SQLitePlugin.js"></script>

    

    <script type="text/javascript">

document.addEventListener("deviceready", onDeviceReady, false);


var db;

    function onDeviceReady() {

    // open database

db = window.sqlitePlugin.openDatabase({name: "MyDB"});

// create table;

db.transaction(function(tx) {

tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

});

// insert data

db.transaction(function(tx) {

     tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {

         console.log("insertId: " + res.insertId + " -- probably 1");

         console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

       });

});

// select data

db.transaction(function(tx) {

            tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {

              console.log("res.rows.length: " + res.rows.length + " -- should be 1");

              console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");

            });

});

}

    

    </script>

  </head>

  <body>

  </body>

</html>



728x90
BLOG main image
"그게 뭐 어쨌다는 거냐?" 늘 누가 나에게 나에대한 말을할 때면 이말을 기억해라. by nobang

카테고리

nobang이야기 (1932)
Life With Gopro (7)
Life With Mini (79)
Diary (971)
너 그거 아니(do you know) (162)
난 그래 (158)
Study (290)
속지말자 (10)
Project (34)
Poem (15)
Song (0)
Photo (113)
낙서장 (45)
일정 (0)
C.A.P.i (2)
PodCast (0)
nobang (27)
고한친구들 (4)
recieve (0)
History (0)
android_app (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :