Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Android sqlite database operation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Sqlite is different from other common databases, that is, sqlite database is saved as a file, which can be exported directly from the phone, stored as a file, and then put on the computer to view.

The steps for Android to operate the database are as follows:

1. Inherit SQLiteOpenHelper and implement the methods in it.

Public class MyDbHelper extends SQLiteOpenHelper {public MyDbHelper (Context context) {super (context, "db3", null, 1);} @ Override public void onCreate (SQLiteDatabase db) {db.execSQL ("create table db (id int primary key autoincrement,name varchar (10), phone varchar (15);");} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL ("alert table db add column yuan varchar (11);") 2. Create a dao layer, which acts like the dao layer in the ssh framework in java, and is used to manipulate the database public class ContactInfoDao {private final MyDbHelper mMyDbHelper; private final SQLiteDatabase mDb; / / construction method. When generating entity objects of this class, the object public ContactInfoDao (Context context) {mMyDbHelper = new MyDbHelper (context) {mMyDbHelper = mMyDbHelper.getWritableDatabase () is generated directly. } / * add contact information * @ param name * @ param phone * / public void add (String name,String phone) {mDb.execSQL ("insert into db3 (name,phone) values (?)", new Object [] {name,phone}); mDb.close ();}}

3. After writing the corresponding method in the dao layer, call the method in dao in Activity for business logic

Package com.yuanlp.createdb3;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.yuanlp.createdb3.dao.ContactInfoDao;public class MainActivity extends AppCompatActivity {private EditText mName; private EditText mPhone; private ContactInfoDao mContactInfoDao; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.layout) MName = (EditText) findViewById (R.id.et_name); mPhone = (EditText) findViewById (R.id.et_phone); mContactInfoDao = new ContactInfoDao (this);} / * * insert database * @ author yuan * @ param view * / public void add (View view) {String name=mName.getText (). ToString (). Trim () String phone=mPhone.getText (). ToString (). Trim (); if (TextUtils.isEmpty (name) | | name==null) {Toast.makeText (this, "name cannot be empty", Toast.LENGTH_SHORT). Show (); return;} else if (TextUtils.isEmpty (phone) | | phone==null) {Toast.makeText (this, "number cannot be empty", Toast.LENGTH_SHORT). Show (); return } mContactInfoDao.add (name,phone); Toast.makeText (this, "insert database successfully", Toast.LENGTH_SHORT). Show ();}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report