How to connect to a Sybase Database using Android?
I want to use data, and put data, in a sybase database using android.
How can I access i开发者_C百科t?
http://iablog.sybase.com/mobiledatabase/2011/05/database-programming-on-android-with-ultralite/
I think this has some nice information on how to do it. Hope it helps!
Assuming you've got the JAR file (as the tutorial says) you can do this.
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());
}
}
精彩评论