2011-09-27 61 views

回答

2

http://iablog.sybase.com/mobiledatabase/2011/05/database-programming-on-android-with-ultralite/

我覺得這對如何做到這一點一些不錯的信息。希望能幫助到你!

假設你有JAR文件(如教程所說),你可以這樣做。

import java.util.Random; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import com.ianywhere.ultralitejni12.*; 

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
try{ 
    // Define the database properties using a Configuration object 
    ConfigFileAndroid config = DatabaseManager.createConfigurationFileAndroid("hello.udb", this); 
    // Connect, or if the database does not exist, create it and connect 
    try{ 
     _conn = DatabaseManager.connect(config); 
    } catch (ULjException e) { 
     _conn = DatabaseManager.createDatabase(config); � 
    } 

    // Create a table T1 in the database if it does not exist 
    StringBuffer sb = new StringBuffer(); 
    sb.append("CREATE TABLE IF NOT EXISTS T1 (C1 integer primary key default autoincrement, C2 integer)"); 
    PreparedStatement ps = _conn.prepareStatement(sb.toString()); 
    ps.execute(); 
    ps.close(); 

    // Insert a row into T1 
    sb = new StringBuffer("INSERT INTO T1 (C2) ON EXISTING SKIP VALUES (?)"); 
    ps = _conn.prepareStatement(sb.toString()); 
    ps.set(1, new Random().nextInt()); 
    ps.execute(); 
    ps.close(); 
    _conn.commit(); 
    // Select the values from C2 and show them in the user interface 
    sb = new StringBuffer("SELECT C2 FROM T1"); 
    ps = _conn.prepareStatement(sb.toString()); 
    ResultSet rs = ps.executeQuery(); 
    StringBuffer c2 = new StringBuffer(); 
    while(rs.next()){ 
     c2.append(String.valueOf(rs.getInt(1))); 
     c2.append(","); 
    } 
    TextView tv = (TextView)findViewById(R.id.textview1); 
    tv.setText(c2.toString()); 
} catch (ULjException e) { 
    Log.e("HelloUltraLite", e.toString()); 
} 

}

+0

感謝傑克,幫助:) –

相關問題