note_3

1 Introduction Database

   sqlite

   Under what circumstances the use of database

    There are large amounts of data need to be stored similar structure when

Creating a database of 2

  Define a class that inherits sqliteOpenhelper

  OpenHelper used to open the database

  Rewrite abstract methods

  

/ ** 
*
* @param context SA
* name: Ming word database
* factory aim to create a cursor object
* version version of the database from the beginning 1
* /


public MyOpenHelper (Context context) {
Super (context, "suzuna.db", null , 1);
}
create objects of OpenHelper
MyOpenHelper helper = new MyOpenHelper(getApplicationContext());

调用
.getWritableDatabase (); 
method to open or create a database, and if the first call is to create a database, such as:
 
SQLiteDatabase database = helper.getWritableDatabase();
 
transfer
 
.getReadableDatabase (); 
method to open or create a database, and if the first call is to create a database, if the disk is full read-only database object is returned, such as:
SQLiteDatabase database = helper.getReadableDatabase();
SQLiteDatabase object provides a method of operation of the database management 


default constructor

Method 3 OnCreate method database and OnUpgrade

  OnCreate: when the database was first created is called, this method is particularly suitable for initialization table structure

  You need to use sql statement to create table

  id usually begins with an underscore _

  db.execSQL( "create table info (_id integer primary key autoincrement , name varchar(20)  )" );

  Db file generated using SQLite Expert Professional Open

  

  OnGrade: when the database is upgraded version, which will be called

  This method is suitable for the update table structure

  For example, to increase a data table information, perform the following sql statement

  db.execSQL("alter table info add phone varchar(20)");

  When you open the same directory db file must have wal shm file and read the contents of less than or database (

4 Use the sql statement to the database additions and deletions to change search

  Enter the execution of SQL in SQLite Expert Professional interface

  Insert data insert into info (name, phone) values ​​( 'www', '1377770')

  Modify the data update info set phone = '2313123' where name = 'www'

  Query data select name, phone from info query name and phone columns from the table info

  Or select * from info query all the columns from the table info

  You can also limit the scope of what line of inquiry

  Returns a cursor object that can traverse a query to a database table

  Delete data delete from info where name = 'www' delete information from the called www info table

  SQL parameter placeholders -?> Then use an array to replace bit   

  

 
  Contents of the database can be operated directly write sql statement is executed, you can also use the direct method APi provided by Google engineer database operation

  Disadvantages: 1 sql statement is easy to mistake

     Sql statement 2 performs no return value is not easily determined

  Advantages: easy multi-table query

  supplement:

     Using the command line tool can open a database Sqlite3

     Enter the command sql database operations

     Changing dos encoding chcp 936 adapted for encoding GBK

     Dos change chco 65001 encoding changed to utf-8 encoded

    

 

5 api packaged using Google database search changes, additions or deletions

   Call the object's method of operation SqlitDatabase

  Add a record

    db.insert(table,nullColimnHack,values);

      Name of the table: table

 

      nullComlimnHack may be null

       ContentValues ​​inside a package name map in which the map is to insert a column key, value the value of this row is inserted

        ContentValues values = new ContentValues();

        values.put(name,values);

        ....

    After inserting the return value represents a successful new line of id, data type is long

  

  Delete Record

    db.delete(tablename,whereClause,WhereArg);

    "name=?"

  update record

    db.update(table,values,whereClause,whereArgs);

    Returns the number of deleted rows int

  inquiry record

    Cursor  c = db.query(...)

 

    The method is passed underlying parameters Assembling sql

    

    Advantages: simple and does not require writing complex and error-prone sql statement not return a value may be determined whether the operation is successful    

  

    Cons: If you have multiple tables using Google api can not be easily packaged inquiry

    

62 Species deletions search changed disadvantages

 

Services introduced 7 Android in the database

   Services: the implementation of a logic, either all succeed or fail  

  Call the method

  db.beginTransition ()

  Using a template provided by Google write the business logic, if an error occurs logic is automatically rolled back to before execution, otherwise success.

The entry-8 listview

  0 define the layout listView

     1 find control by id

     findViewById

     2 shows the data and common controls are not the same, data from the data adapter ListAdapter

      ListViewAdapter is the interface can not be used directly, so you can own or use his implementation class

       For example, can be inherited from BaseAdapter and four abstract methods

   1 definition of a data adapter listView

   2 getCount getView method and the method implemented baseAdapter

    GetCount method is responsible for controlling the number of entries to be displayed

    getView method is responsible for controlling each entry to show what controls

   The adapter 3 is disposed listView

9 listview optimization

   GetView method will be called upon when listView entry into the screen is displayed, but if the entry is a lot of fast display, memory overflow error occurs, because the new View object has not been enough time to clean up, the program stops running

  This is when you need to use converVew argument, he represented the previously used View, called the history cache object, if it is empty, that is first displayed, you can only create a new View to display content, and content to be displayed after ConverView can be used directly without re-created, so as to avoid because creating too many objects cause memory overflow View

10 listview display principle data

   MVC

  JavaWeb:

  M: mode data

  V: View View jsp

  C: controller controller servlet

  Android:

  M: mode data (javaBean)

  V:View listView

  C: adapter adapter

  The best listView high to match the parent space

11 listview display complex pages

   The preparation of the interface to be displayed as a layout file, then through the pump api View this layout file into a file, and then returns the process getView displayed as entry listView

  Linear layout relative layout inherits from ViewGroup, description of these components can have their own sub-layout, which can be put in other components and sub-layout, rather than inherited from ViewGroup controls such as TextView, etc. can not put other controls inside

  Use View.inflate methods

 

12 pump used to obtain api

   Pump method for obtaining services

  1。 View  v   =  View.inflate()

  2. View view = LayoutInflater.from().inflate()

  3. LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);

    View v  =   inflater.inflate();

13 arrayAdapter use

   1 Definitions listView

  2 declaration Adapter 

    如 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item,objects)

    item layout file is a list of items for displaying data

  3 Set adapter

14 simpleAdapter use

  

  1 Definitions listView

  2 Prepare the required data, List <? Extends Map <String,? >> data

    2 declaration Adapter 

      如 SimpleAdapter adapter = new SimpleAdapter(context,data,resource,from,to);

      context is the context where listView

      data is the data to be displayed, he is a collection of a set of map objects

      resource is a list of items item layout file

      from a String [] object stored key map key pairs

      It is to int [] id item stored inside each component, id corresponding key that corresponds to the content for display

      item layout file is a list of items for displaying data

  3 Set adapter

 

Data out 15 query the database to display on listView

   

  0 lv Found

  1 from the database to check out the data, encapsulated in javabean

  2 javabean data displayed on listview

  Setting data adapter 3

 

16 summary

  How to create a database

  Define a class that inherits SqliteOpenHelper

  SQLiteDatabase; operation of the database 

  

  oncreate method: When you first create a database call, suitable for a database table initialization

  onupgrade: When the database is updated version is called

  

  The first way to change the database search deletions

  Traditional sql statement

  Google engineers provided us with api master the operation of the database

  Command-line tool can open a database sqlite3 practice

  Transaction database, practice

 

  listview display master data

  baseAdapter master

  Gets pump grasp of common api

  1 View.inflate

  2 LayoutInflater.from(...).inflate()

  3 LayoutInflater inflater = context.getSystemService(LAYOUT_INFLATE_SERVICE);

     view = inflater.inflate(...);

 

  ArrayAdapter master

  simpleAdapter understand

Guess you like

Origin www.cnblogs.com/suzuna/p/11182638.html