Android sqlite local database

Source: Internet
Author: User

The same is true for Android databases used for any development. Android generally uses two databases: local database and remote database. The local database is sqlite, I usually use mysql. This time, I first learned about the local database sqlite. The general database type is varchar (20). If you input a data greater than 20 bits, an error is returned, you can't insert it, but it's different in sqlite. It's one of the characteristics of sqlite. Let's take a look at the crud of sqlite:

MainActivity

package com.example.sqlite;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView t;                DBOpenHelter dbopenhelper=new DBOpenHelter(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}


PersonService

Package com. example. sqlite; import com. example. bean. person; import android. content. context; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; public class PersonService {private DBOpenHelter dbOpenHelper; public PersonService (Context context) {this. dbOpenHelper = new DBOpenHelter (context);} public void save (Person person) {SQLiteDatabase db = dbOpenHelper. getWritableDatabase (); Strin GBuilder SQL = new StringBuilder (128); // This SQL is useless. append ("insert"); // This does not use db.exe cSQL ("insert into person (name, phone) values (?,?) ", New Object [] {person. getName (), person. getPhone ()}); db. close () ;}public Person find (Integer id) {Person person = new Person (); SQLiteDatabase db = dbOpenHelper. getReadableDatabase (); Cursor cursor = db. rawQuery ("select * from testpersion wehre name =? ", New String [] {id. toString ()}); while (cursor. moveToNext () {int personid = cursor. getInt (cursor. getColumnIndex ("id");} return person ;}}

DBOpenHelter

Package com. example. sqlite;

Import android. content. Context;
Import android. database. sqlite. SQLiteDatabase;
Import android. database. sqlite. SQLiteDatabase. CursorFactory;
Import android. database. sqlite. SQLiteOpenHelper;

Public class DBOpenHelter extends SQLiteOpenHelper {

Public DBOpenHelter (Context context ){
Super (context, "itcast. db", null, 1 );
}

@ Override
Public void onCreate (SQLiteDatabase db ){
Db.exe cSQL ("create table testpersion (persionid integer primary key autoincrement, name varchar (20 ))");
}

@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
// TODO Auto-generated method stub
Db.exe cSQL ("alter table testpersion ADD phone VARCHAR (12) NULL ");
}

}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.