Android Basic database creation and use

Source: Internet
Author: User

The four components of Android have content Provider, which provides their own data for other applications, so, in general, Android apps do not need to provide content Provider.

1. Simple Database form Field declaration:

Mysqlname.java

    

         Public Static Final intVERSION = 1;  Public Static FinalString DBNAME = "MyDB"; /** Contact details Database*/     Public classcontacts{ Public Static FinalString tablename= "Contacts";  Public Static FinalString id= "CId";//URI of the contact person                 Public Static FinalString number= "Number";  Public Static FinalString name= "Name"; }

Generally a table, write an inner class, the variables of the inner class are the columns in the database. This file will do as it is written.

2. Create a database

Mysql.java

  

Public class Mysql extends sqliteopenhelper{

PublicMysql (Context context) {Super(Context,mysqlname.dbname,NULL, mysqlname.version); } @Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method Stub /** Create a people form*/Db.execsql ("CREATE TABLE" +mysqlname.contacts.tablename+ "(" +Mysqlname.Contacts.Id+ "Integer PRIMARY key autoincrement NOT NULL," +Mysqlname.Contacts.Number+ "text," +Mysqlname.Contacts.Name+ "text);"
  
); }
}

In this way, the creation of the database is completed, but in addition to the creation of work, but also to provide some external methods;

3. Operation

    • Inquire
This null, NULL null);

If you need to use the values within this CR, you need to:

if (cr!=null&&cr.getcount () >0&&= cr.getstring (Cr.getcolumnindex ( Mysqlname.Contact.Name));}

This must be movetofirst, or it will be an error.

    • Insert
Db.insert (Mysqlname.Contact.TableName, Mysqlname. Contact.id, values)

    • Delete
Db.delete (Mysqlname.Contact.TableName, mysqlname.contact.id+ "=? and" +mysqlname.contact.name+ "=?", New String[]{id, name});

    • Modify
New String[]{id})

Note that the above db, Cursor, after use, must be called in Time Db.close (), Cr.close ();

  

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.