How to Use Android learning data (2)

Source: Internet
Author: User

How to Use Android learning data (2)

I learned SQLite in the afternoon and used it as a very simple information management system. After the system was complete, I felt that all the single-host management systems should be ready !!!!

Now let's talk about the usage of SQLite (add, delete, query, modify)

I will not talk about anything about it. Baidu has made a big push. I also want to talk about it. Since it is a database, I like graphical operations. I found one online. I think it's good to recommend it to everyone.

Http://yunpan.cn/Q7bYmCQucJ9qU extraction code fb99

The interface is like this.


It's easy to use!

SQLite provides a class SQLiteOpenHelper, which can be used as a tool class <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/4sP7us2w5rG + usWho7T6wuvI58/Co7o8L3A + release "brush: java;"> public class DBOpenHelper extends SQLiteOpenHelper {private static final String DBNAME = "maid. db "; private static final int VERSION = 1; private static final String TAG =" DBOpenHelper ";/***** @ param context * Constructor (Database Name and VERSION number) */public DBOpenHelper (Context context) {super (context, DBNAME, null, VERSION) ;}@ Override/** (non-Javadoc) * @ see android. database. sqlite. SQLiteOpenHelper # onCreate (android. database. sqlite. SQLiteDatabase) * generally refers to TABLE creation and default data */public void onCreate (SQLiteDatabase db) {String SQL = "CREATE TABLE table_student (sid integer primary key, name varchar (20 ), age inter); "mongodb.execsql(sql1_mongodb.exe cSQL (" insert into table_student values (1, 'xiongba Tian', 20) "mongomongodb.exe cSQL (" insert into table_student values (2, 'blackyun ', 21) "mongodb.exe cSQL (" insert into table_student values (3, 'handsome Liu ', 22) "); Log. I (TAG, "onCreate") ;}@ Override/*** is generally used for application upgrade and Database Upgrade **/public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {Log. I (TAG, "onUpgrade ");}}Let's take a look at the above Code, which is very simple.

super(context,DBNAME, null, VERSION);
Specify the name of the database. It is better to add ". db" such as "student. db" to the database name so that it can be regarded as a standard database file.

The default version number is 1.

The database statements are still SQL statements and remain unchanged. The change is how to run these SQL statements.

The following describes (the most basic and basic running method)

You can use SQL statements that do not return numeric values, such as add or delete.

db.execSQL(sql);db.execSQL("update table_student set name="+name+",age="+age+" where sid="+id);db.execSQL(sql, Object[]);db.execSQL("update table_student set name=?,age=? where sid=?",new Object[]{name,age,id});
For the same effect, the second type is recommended!

How does db come from here?

Write this when DBOpenHelper is used in the Model.

private DBOpenHelper helper;private SQLiteDatabase db;public StudentDao(Context context){helper = new DBOpenHelper(context);db = helper.getWritableDatabase();}
Then you can use the database as needed.

This is required when you need to extract values from the database.

/* Get all students */public List
 
  
GetAllList () {List
  
   
List = new ArrayList
   
    
(); Cursor cursor = db. rawQuery ("select * from table_student"}); while (cursor. moveToNext () {Student student = new Student (cursor. getInt (0), cursor. getString (1), cursor. getShort (2); list. add (student);} return list ;}
   
  
 
Note that this Cursor is similar to the Result in SQL. SQL statements require query. Of course, there are other methods, such

Cursor cursor = db.query("table_student", null, null, null, null, null, null);
Writing in this way is a little complicated, not like an SQL statement. In this way, there are many parameters.

I like the first one

Don't forget to close the database, or the computer will become stuck!

Now SQLite is an entry-level system, and the basic addition, deletion, query, and modification operations are all completed. The management system can be easily written !!!!








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.