如何使用Android连接到Sybase数据库?

时间:2011-09-27 08:41:42

标签: android database sybase

我想使用android来使用数据并将数据放入sybase数据库。

我如何访问它?

1 个答案:

答案 0 :(得分: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());
}

}

相关问题