인 스 턴 스 튜 토리 얼 6:데이터베이스 생 성 및 데이터 추가 삭제 검사 완료-첫 번 째 쓰기

문자 조수(소스 코드 첨부)http://www.eoeandroid.com/thread-207835-1-1.html
비망록+SQLite 의 실현http://www.eoeandroid.com/thread-208013-1-1.html
음악 파형 도http://www.eoeandroid.com/thread-207796-1-1.html
<?xml version="1.0" encoding="utf-8"?>     <manifest xmlns:android="http://schemas.android.com/apk/res/android"         package="cn.itcast.db"         android:versionCode="1"         android:versionName="1.0" >           <uses-sdk android:minSdkVersion="8" />           <application             android:icon="@drawable/ic_launcher"             android:label="@string/app_name" >             <activity                 android:name=".MainActivity"                 android:label="@string/app_name" >                 <intent-filter>                     <action android:name="android.intent.action.MAIN" />                       <category android:name="android.intent.category.LAUNCHER" />                 </intent-filter>             </activity>             <uses-library android:name="android.test.runner"/>         </application>           <instrumentation android:name="android.test.InstrumentationTestRunner"             android:targetPackage="cn.itcast.db" android:label="Tests for My App"></instrumentation>           </manifest>
package cn.itcast.db;       import android.app.Activity;     import android.os.Bundle;       public class MainActivity extends Activity {         /** Called when the activity is first created. */         @Override         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.main);         }               // Android   ,              ——SQLite         //SQLite3  NULL、INTEGER、REAL(    )、TEXT(     ) BLOB(     )             //          5 ,    sqlite3   varchar(n)、char(n)、decimal(p, s)            //                               //SQLite                         ,                      }
package cn.itcast.domain;       public class Person {             private Integer id;             private String name;             private String phone;                               public Person(String name, String phone){                     this.name = name;                     this.phone = phone;             }                       public Person(Integer id, String name, String phone){                     this.id = id;                     this.name = name;                     this.phone = phone;             }                       public Integer getId() {                     return id;             }             public void setId(Integer id) {                     this.id = id;             }             public String getName() {                     return name;             }             public void setName(String name) {                     this.name = name;             }             public String getPhone() {                     return phone;             }             public void setPhone(String phone) {                     this.phone = phone;             }          }   
package cn.itcast.service;     import android.content.Context;  import android.database.sqlite.SQLiteDatabase;  import android.database.sqlite.SQLiteOpenHelper;     public class DBOpenHelper extends SQLiteOpenHelper{             private static final int VERSION = 2; //                         public DBOpenHelper(Context context){                   super(context, "itcast.db", null, VERSION); //< >/database/             }             @Override             public void onCreate(SQLiteDatabase db) { //                                      db.execSQL("CREATE TABLE person(personId integer primary key autoincrement, " + "name varchar(20))");                          }             @Override             public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //                                   db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL");             }     }   
package cn.itcast.service;       import java.util.ArrayList;  import java.util.List;  import android.content.Context;  import android.database.Cursor;  import android.database.sqlite.SQLiteDatabase;  import cn.itcast.domain.Person;     public class PersonService {             private DBOpenHelper dbOpenHelper;                public PersonService(Context context){                     this.dbOpenHelper = new DBOpenHelper(context);           }                     /**            *                 * @param person            */             public void save(Person person){                     SQLiteDatabase db = dbOpenHelper.getWritableDatabase();                   db.execSQL("insert into person(name, phone) values(?, ?)",new Object[]{person.getName(), person.getPhone()});                     //          db,      ,                       ////                db.close();           }                     /**            *                 * @param id   ID            */             public void delete(Integer id){                     SQLiteDatabase db = dbOpenHelper.getWritableDatabase();                   db.execSQL("delete from person where personId=?", new Object[]{id});             }                    /**            *                 * @param person           */             public void update(Person person){                     SQLiteDatabase db = dbOpenHelper.getWritableDatabase();                   db.execSQL("update person set name=?, phone=? where personId=?",                                   new Object[]{person.getName(), person.getPhone(), person.getId()});             }                     /**           *                 * @param id   ID            * @return            */             public Person find(Integer id){                     SQLiteDatabase db = dbOpenHelper.getReadableDatabase();                   Cursor cursor = db.rawQuery("select * from person where personId=?", new String[]{id.toString()});                     if(cursor.moveToFirst()){                             int personId = cursor.getInt(cursor.getColumnIndex("personId"));                           String name = cursor.getString(cursor.getColumnIndex("name"));                           String phone = cursor.getString(cursor.getColumnIndex("phone"));                           return new Person(personId, name, phone);                   }                     cursor.close();                                return null;           }                     /**           *                   * @param offset                       * @param maxResult                      * @return            */             public List<Person> getScrollData(int offset, int maxResult){                     List<Person> persons = new ArrayList<Person>();                     SQLiteDatabase db = dbOpenHelper.getReadableDatabase();                  Cursor cursor = db.rawQuery("select * from person order by personId asc limit ?,?",                                   new String[]{String.valueOf(offset), String.valueOf(maxResult)});                     while(cursor.moveToNext()){                             int personId = cursor.getInt(cursor.getColumnIndex("personId"));                          String name = cursor.getString(cursor.getColumnIndex("name"));                          String phone = cursor.getString(cursor.getColumnIndex("phone"));                           persons.add(new Person(personId, name, phone));                  }                     cursor.close();                return null;             }                       /**            *                  * @return
*/ public long getCount(){ SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select count(*) from person", null); cursor.moveToFirst(); long result = cursor.getLong(0); cursor.close(); return result; } }
package cn.itcast.test;       import java.util.List;  import cn.itcast.domain.Person;   import cn.itcast.service.DBOpenHelper;   import cn.itcast.service.PersonService;   import android.test.AndroidTestCase;   import android.util.Log;       public class PersonServiceTest extends AndroidTestCase{             private static final String TAG = "PersonServiceTest";             public void testCreateDB() throws Exception{                     DBOpenHelper dbOpenHelper = new DBOpenHelper(getContext());                  dbOpenHelper.getWritableDatabase();           }                    public void testSave() throws Exception{                     PersonService service = new PersonService(this.getContext());                   for(int i = 0; i < 20; i++){                             Person person = new Person("zhangsan", "123456");                          service.save(person);                   }           }             public void testDelete() throws Exception{                     PersonService service = new PersonService(this.getContext());                  service.delete(21);           }             public void testUpdate() throws Exception{                     PersonService service = new PersonService(this.getContext());                  Person person = service.find(1);                  person.setName("zhangxiaoxiao");                  service.update(person);          }             public void testFind() throws Exception{                     PersonService service = new PersonService(this.getContext());                  Person person = service.find(1);                  Log.i(TAG, person.toString());             }            public void testScrollData() throws Exception{                     PersonService service = new PersonService(this.getContext());                  List<Person> persons = service.getScrollData(0, 5);                  for(Person person : persons){                           Log.i(TAG, person.toString());                   }           }               public void testCount() throws Exception{                     PersonService service = new PersonService(this.getContext());                  long result = service.getCount();                   Log.i(TAG, result + "");           }             //  execSQL() rawQuery  ,         、  、  、                  //insert(), delete(), update(), query()     }   

좋은 웹페이지 즐겨찾기