Danh mục

Android kiên trì: Cơ sở dữ liệu SQL

Số trang: 81      Loại file: pdf      Dung lượng: 1.37 MB      Lượt xem: 10      Lượt tải: 0    
10.10.2023

Phí tải xuống: 32,000 VND Tải xuống file đầy đủ (81 trang) 0
Xem trước 9 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Android (cũng như hệ điều hành iPhone) sử dụng một chương trình độc nhúng gọi là SQLite3 có thể được sử dụng để: tạo ra một cơ sở dữ liệu, xác định SQL bảng biểu, chỉ tiêu, các truy vấn, xem, gây nên Chèn hàng, xóa hàng, hàng thay đổi, các truy vấn chạy và quản lý một tập tin cơ sở dữ liệu SQLite.
Nội dung trích xuất từ tài liệu:
Android kiên trì: Cơ sở dữ liệu SQL Android Persistency: SQL DatabasesNotes are based on: Android Developers http://developer.android.com/index.html SQL DatabasesUsing SQL databases in Android.Android (as well as iPhone OS) uses an embedded standaloneprogram called sqlite3 which can be used to: create a database, Insert rows, define SQL tables, delete rows, indices, change rows, queries, run queries and views, administer a SQLite database triggers file. 2 SQL DatabasesUsing SQLite1. SQLite implements most of the SQL-92 standard for SQL.2. It has partial support for triggers and allows most complex queries (exception made for outer joins).3. SQLITE does not implement referential integrity constraints through the foreign key constraint model.4. SQLite uses a relaxed data typing model.5. Instead of assigning a type to an entire column, types are assigned to individual values. This is similar to the Variant type in Visual Basic.6. Therefore it is possible to insert a string into numeric column and so on. Documentation on SQLITE available at http://www.sqlite.org/sqlite.html Good GUI tool for SQLITE available at: http://sqliteadmin.orbmu2k.de/ 3 SQL DatabasesHow to create a SQLite database?Method 1public static SQLiteDatabase.openDatabase ( String path, SQLiteDatabase.CursorFactory factory, int flags ) Open the database according to the flags OPEN_READWRITE OPEN_READONLYCREATE_IF_NECESSARY . Sets the locale of the database to the the systemscurrent locale.Parameterspath to database file to open and/or createfactory an optional factory class that is called to instantiate a cursor when query is called, or null for defaultflags to control database access modeReturns the newly opened databaseThrows SQLiteException if the database cannot be opened 4 SQL DatabasesExample 1. Create a SQLite Databasepackage cis493.sqldatabases;import android.app.Activity;import android.database.sqlite.*;import android.os.Bundle;import android.widget.Toast;public class SQLDemo1 extends Activity {SQLiteDatabase db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // filePath is a complete destination of the form // /data/data// // /sdcard/ try { db = SQLiteDatabase.openDatabase( /data/data/cis493.sqldatabases/myfriendsDB, null, SQLiteDatabase.CREATE_IF_NECESSARY); db.close(); } catch (SQLiteException e) { Toast.makeText(this, e.getMessage(), 1).show(); } }// onCreate 5}//class SQL DatabasesExample 1. Create a SQLite DatabaseAndroid’sSystemImageDevice’s memory 6 SQL Databases Example 1. Create a SQLite Database Creating the database file in the SD cardUsing:db = SQLiteDatabase.openDatabase( sdcard/myfriendsDB, null, SQLiteDatabase.CREATE_IF_NECESSARY); 7 SQL DatabasesWarning1. Beware of sharing issues. You cannot access other people’s database resources (instead use Content Providers or SD resident DBs).2. An SD resident database requires the Manifest to include: NOTE: SQLITE (as well as most DBMSs) is not case sensitive. 8 SQL DatabasesExample2An alternative way of opening/creating a SQLITE database in your localAndroid’s data space is given below SQLiteDatabase db = this.openOrCreateDatabase( myfriendsDB, MODE_PRIVATE, null);where the assumed prefix for the database stored in the devices ram is:/data/data//databases/. For instance if this app iscreated in a namespace called “cis493.sql1”, the full name of the newly createddatabase will be: “/data/data/cis493.sql1/databases/myfriendsDB”.This file could later be used by other activities in the app or exported out of theemulator (adb push…) and given to a tool such as SQLITE_ADMINISTRATOR (seenotes at the end ...

Tài liệu được xem nhiều: