인 스 턴 스 튜 토리 얼 6:데이터베이스 생 성 및 데이터 추가 삭제 검사 완료-첫 번 째 쓰기
15354 단어 데이터 뱅 크이동 개발안 드 로 이 드 개발
비망록+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() }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
nginx websocket ip_해시 규칙프로젝트 를 다운로드 한 후 서로 다른 네트워크 에 각각 이 demo 프로젝트 를 배치 합 니 다. 프로젝트 에서 환경 변수 에 따라 시스템 변 수 를 설정 합 니 다. spring.profiles.active=de...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.