Techbox Android 05 Database
Techbox Android 05 Database
Database
Projeto TechBox | Database
http://www.ibm.com/developerworks/xml/library/x-
androidstorage/index.html
Iniciando o DBHelper
private SQLiteDatabase db;
private static final int DATABASE_VERSION = 1;
private static final String DB_NAME = "sample.db";
private static final String TABLE_NAME = "friends";
Usando o SQLite
http://android10.org/index.php/articlesdatastorage/231-using-the-sqlite-
database-with-listview
1 public class DatabaseHelper extends SQLiteOpenHelper {
2
3 public DatabaseHelper(Context context) {
4 super(context, "CursorDemo", null, 1);
5 }
6
7 @Override
8 public void onCreate(SQLiteDatabase db) {
9 db.execSQL("CREATE TABLE IF NOT EXISTS names ("
10 + BaseColumns._ID
11 + " INTEGER PRIMARY KEY AUTOINCREMENT, first VARCHAR, last
12 db.execSQL("INSERT INTO names (first, last) VALUES ('John', 'Doe')");
13 db.execSQL("INSERT INTO names (first, last) VALUES ('James', 'Kirk')");
14 }
15
16 @Override
17 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
18 // Steps to upgrade the database for the new version ...
19 }
20 }
23 }
http://android10.org/index.php/articlesdatastorage/235-creating-and-
using-databases-in-android-one
33 @Override
34 public void onCreate(SQLiteDatabase db)
35 {
36 db.execSQL(DATABASE_CREATE);
37 }
38
39 @Override
40 public void onUpgrade(SQLiteDatabase db, int oldVersion,
41 int newVersion)
42 {
43 Log.w(TAG, "Upgrading database from version " + oldVersion
44 + " to "
45 + newVersion + ", which will destroy all old data");
46 db.execSQL("DROP TABLE IF EXISTS titles");
47 onCreate(db);
48 }
49 }
50
51 public DBAdapter open() throws SQLException
52 {
53 db = DBHelper.getWritableDatabase();
54 return this;
55 }
56
57 public void close()
58 {
59 DBHelper.close();
60 }
61
62 public long insertTitle(String isbn, String title, String publisher)
63 {
64 ContentValues initialValues = new ContentValues();
65 initialValues.put(KEY_ISBN, isbn);
66 initialValues.put(KEY_TITLE, title);
67 initialValues.put(KEY_PUBLISHER, publisher);
68 return db.insert(DATABASE_TABLE, null, initialValues);
69 }
70
71 public boolean deleteTitle(long rowId)
72 {
73 return db.delete(DATABASE_TABLE, KEY_ROWID +
74 "=" + rowId, null) > 0;
75 }
76
77 public Cursor getAllTitles()
78 {
79 return db.query(DATABASE_TABLE, new String[] {
80 KEY_ROWID,
81 KEY_ISBN,
82 KEY_TITLE,
83 KEY_PUBLISHER},
84 null, null, null, null, null);
85 }
86
87 public Cursor getTitle(long rowId) throws SQLException
88 {
89 Cursor mCursor =
90 db.query(true, DATABASE_TABLE, new String[] {
91 KEY_ROWID,
92 KEY_ISBN,
93 KEY_TITLE,
94 KEY_PUBLISHER },
95 KEY_ROWID + "=" + rowId,
Projeto TechBox | Database
Usando o Database
1 public class DatabaseActivity extends Activity {
2 @Override
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.main);
6 DBAdapter db = new DBAdapter(this);
7 }
8 }
Adicionando um Ttulo
1 @Override
2 public void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.main);
5
6 DBAdapter db = new DBAdapter(this);
7
8 db.open();
9 long id;
10 id = db.insertTitle(
11 "0470285818",
12 "C# 2008 Programmer's Reference",
13 "Wrox");
14 id = db.insertTitle(
15 "047017661X",
16 "Professional Windows Vista Gadgets Programming",
17 "Wrox");
18 db.close();
19 }
Projeto TechBox | Database
Atualizando um Ttulo
1 @Override
2 public void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.main);
5 DBAdapter db = new DBAdapter(this);
6
7 db.open();
8 if (db.updateTitle(1,
9 "0470285818",
10 "C# 2008 Programmer's Reference",
11 "Wrox Press"))
12 Toast.makeText(this, "Update successful.",
13 Toast.LENGTH_LONG).show();
14 else
15 Toast.makeText(this, "Update failed.",
16 Toast.LENGTH_LONG).show();
17
18 Cursor c = db.getTitle(1);
19 if (c.moveToFirst())
20 DisplayTitle(c);
21 else
22 Toast.makeText(this, "No title found",
23 Toast.LENGTH_LONG).show();
24 db.close();
25 }
Apagando um Ttulo
1 @Override
2 public void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.main);
5 DBAdapter db = new DBAdapter(this);
6
7 db.open();
8 if (db.deleteTitle(1))
9 Toast.makeText(this, "Delete successful.",
10 Toast.LENGTH_LONG).show();
11 else
12 Toast.makeText(this, "Delete failed.",
13 Toast.LENGTH_LONG).show();
14 db.close();
15 }
Atualizando o Banco
1 public class DBAdapter
2 {
3 public static final String KEY_ROWID = "_id";
4 public static final String KEY_ISBN = "isbn";
5 public static final String KEY_TITLE = "title";
Projeto TechBox | Database