CS185c
Chris Pollett
Sep. 29, 2010
~/Library/Application Support/iPhoneSimulator/User/
Window > Show View > Other... > Android > File Explorer.
File testFile = new File("data/data/org.pollett/files/filename.txt");
//another useful directory is /sdcard for the sd card in the device
//if it exists. res/raw is also a useful directory for resource files
FileInputStream fis = null;
if(testFile.exists())
{
try
{
fis = this.openFileInput("filename.txt");
byte[] reader = new byte[fis.available()];
while(fis.read(reader) != -1){}
Log.i("AndroidPersistence", "I just read:\n" +
new String(reader) );
}
catch (IOException ie)
{
Log.e("AndroidPersistence", "File Read Error");
}
finally
{
if(fis !=null)
{
try {fis.close();} catch(Exception e) {}
}
}
}
FileOutputStream fos = null;
try
{
fos = this.openFileOutput("filename.txt", Context.MODE_PRIVATE);
fos.write("hi there\n".getBytes());
Log.i("AndroidPersistence", "I just wrote stuff\n");
}
catch (IOException ie)
{
Log.e("AndroidPersistence", "File Write Error");
}
finally
{
if(fos !=null)
{
try {fos.close();} catch(Exception e) {}
}
}
CREATE TABLE IF NOT EXISTS
EMPLOYEE (ID INTEGER PRIMARY KEY, NAME TEXT, AGE INTEGER)
DROP TABLE IF EXISTS MY_TABLE
INSERT OR REPLACE INTO EMPLOYEE (ID, NAME, AGE) VALUES (5, 'Bob', 30)Note this s
DELETE FROM tablename WHERE condAn example condition might be age: > 25
SELECT col_name1, col_name2, ..., col_name_n /*to empahsize what table do: table_name.col_name */ FROM table_name_1, table_name_2, ..., table_name_m WHERE condan example condition might be: EMPLOYEE.ID = PROJECT.directorID AND PROJECT.budget = 1000000
sqlite3 *database;
int result = sqlite3_open("/path/to/database", &database);
char *cStringPath = [someStringObj UTF8String];
sqlite3_close(database);
char *errorMsg; const char *createSQL ="CREATE TABLE IF NOT EXISTS PEOPLE (ID INTEGER PRIMARY KEY, HOBBY TEXT)"; int result = sqlite3_exec(database, createSQL, NULL, NULL, &errorMsg);
NSString *query = @"SELECT * FROM PEOPLE";
sqlite3_stmt *statement;
int result = sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil);
//could check if result is SQLITE_OK
while(sqlite3_step(statement) == SQLITE_ROW){
// for the current row let's get out some columns
int id = sqlite3_column_int(statement, 0); //0th column's data
char *hobby = (char*)sqlite3_column_text(statement, 1);
// do something with the data for this row
}
sqlite3_finalize(statement);
import android.database.sqlite.*; import android.database.*; import android.content.*; import android.util.*;
dbOpenHelper = new DBOpenHelper(this, "My_Database", 3);
db = dbOpenHelper.getWritableDatabase();
//insert into database
db.execSQL("INSERT OR REPLACE INTO PEOPLE VALUES (5, 'test')");
db.close();
dbOpenHelper = new DBOpenHelper(this, "My_Database", 3);
db = dbOpenHelper.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM PEOPLE", null);
for(int i=0; i< c.getCount(); i++)
{
c.moveToNext();
Log.i("SqliteAndroid", "Id="+c.getInt(0));
Log.i("SqliteAndroid", "Hobby="+c.getString(1));
}
db.close();
private static class DBOpenHelper extends SQLiteOpenHelper
{
public DBOpenHelper(Context context, String dbName, int version )
{
// third argument id to a cursor factory -- we don't care about
super(context, dbName, null, version);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL("CREATE TABLE IF NOT EXISTS PEOPLE (ID INTEGER PRIMARY KEY, HOBBY TEXT)");
}
catch(SQLException e)
{
Log.e("SqliteAndroid", "DBOpenHelper", e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS PEOPLE");
this.onCreate(db);
}
}
package org.pollett.TimerTest;
import org.pollett.TimerTest.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Handler;
public class TimerTest extends Activity {
Handler handler = new Handler(); //used to set up a delayed callback
SeekBar seekBar; //object associated with SeekBar on our Layour
TextView progressLabel; //object associated with our TextView label
AlertDialog alertDialog; //object used to hold our Alert
//Runnable object called after delay via Handler
Runnable alertTask = new Runnable() {
public void run() {
alertDialog.show();
}
};
/**
Called when the activity is first created.
Sets up all our the event listeners used in our example
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the seekBar events to modify the label value
progressLabel = (TextView)this.findViewById(R.id.TextView01);
seekBar = (SeekBar)this.findViewById(R.id.SeekBar01);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
progressLabel.setText(""+progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
//we're not doing anything here
}
public void onStopTrackingTouch(SeekBar seekBar) {
//we're not doing anything here
}
});
//set up our Alert Dialog but don't display it yet
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You've waited long enough!").setCancelable(true);
alertDialog = builder.create();
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
// set up button, when button is clicked, use handler to schedule a task
Button waitButton = (Button)this.findViewById(R.id.Button01);
waitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
handler.removeCallbacks(alertTask);
handler.postDelayed(alertTask, seekBar.getProgress()*1000);
}
});
}
}