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

How to use ContentValues to operate the database

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

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use ContentValues to operate the database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In main.xml:

Create a table operation class Mytab.java for creating a database

Package com.li.sqlite

Import android.content.ContentValues

Import android.database.sqlite.SQLiteDatabase

Public class Mytab {

Private static final String TABLENAME = "mytab"; / / indicates the name of the data table to be operated on

Private SQLiteDatabase db = null; / / database operation

Public Mytab (SQLiteDatabase db) {

This.db = db

}

Public void insert (String name,String birthday) {/ / add data to the table

ContentValues cv = new ContentValues ()

Cv.put ("name", name)

Cv.put ("birthday", birthday)

This.db.insert (TABLENAME, null, cv)

This.db.close ()

}

Public void update (int id, String name, String birthday) {/ / modify table data

ContentValues cv = new ContentValues ()

Cv.put ("name", name)

Cv.put ("birthday", birthday)

String whereCaluse = "id=?"

String whereArgs [] = new String [] {String.valueOf (id)}

This.db.update (TABLENAME, cv, whereCaluse, whereArgs)

This.db.close ()

}

Public void delete (int id) {/ / delete the data of the table

String whereCaluse = "id=?"

String whereArgs [] = new String [] {String.valueOf (id)}

This.db.delete (TABLENAME, whereCaluse, whereArgs)

This.db.close ()

}

}

In the MyDatabaseHelper.java class:

Package com.li.sqlite

/ / Auxiliary operation class of database

Import android.content.Context

Import android.database.sqlite.SQLiteDatabase

Import android.database.sqlite.SQLiteOpenHelper

Public class MyDatabaseHelper extends SQLiteOpenHelper {

Private static final String DATABASENAME = "liyewen.db"

Private static final int DATABASERVERSION = 1; / / set the version of the database

Private static final String TABLENAME = "mytab"

Public MyDatabaseHelper (Context context) {/ / users are definitely only concerned about Context

Super (context, DATABASENAME, null, DATABASERVERSION)

}

@ Override

Public void onCreate (SQLiteDatabase db) {/ / create datasheet

String sql = "CREATE TABLE" + TABLENAME + "("

+ "id INTEGER PRIMARY KEY," / / set to Integer in SQLite, PRIMARY KEY will automatically increase ID

+ "name VARCHAR (50) NOT NULL,"

+ "birthday DATE NOT NULL" + ")"

Db.execSQL (sql); / / execute SQL

System.out.println ("* create: onCreate ().")

}

@ Override

Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {

String sql = "DROP TABLE IF EXISTS" + TABLENAME

Db.execSQL (sql)

System.out.println ("* Update: onUpgrade ().")

This.onCreate (db)

}

}

In MySQLiteDemo.java:

Package com.li.sqlite

Import android.app.Activity

Import android.database.sqlite.SQLiteOpenHelper

Import android.os.Bundle

Import android.view.View

Import android.view.View.OnClickListener

Import android.widget.Button

Public class MySQLiteDemo extends Activity {

Private Button inserBut = null

Private Button updateBut = null

Private Button deleteBut = null

Private SQLiteOpenHelper helper = null

Private Mytab mtab = null

Private static int count = 0

@ Override

Public void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

Super.setContentView (R.layout.main)

This.inserBut = (Button) super.findViewById (R.id.insertBut)

This.updateBut = (Button) super.findViewById (R.id.updateBut)

This.deleteBut = (Button) super.findViewById (R.id.deleteBut)

This.helper = new MyDatabaseHelper (this)

This.inserBut.setOnClickListener (new InertOnClickListenerImpl ())

This.updateBut.setOnClickListener (new UpdateOnClickListenerImpl ())

This.deleteBut.setOnClickListener (new DeleteOnClickListenerImpl ())

}

Private class InertOnClickListenerImpl implements OnClickListener {

Public void onClick (View v) {

MySQLiteDemo.this.mtab = new Mytab (

MySQLiteDemo.this.helper.getWritableDatabase ()

MySQLiteDemo.this.mtab.insert ("liyewen" + count++, "1988-08-16")

}

}

Private class UpdateOnClickListenerImpl implements OnClickListener {

Public void onClick (View v) {

MySQLiteDemo.this.mtab = new Mytab (

MySQLiteDemo.this.helper.getWritableDatabase ()

MySQLiteDemo.this.mtab.update (72, "update", "1988-8-15")

}

}

Private class DeleteOnClickListenerImpl implements OnClickListener {

Public void onClick (View v) {

MySQLiteDemo.this.mtab = new Mytab (

MySQLiteDemo.this.helper.getWritableDatabase ()

MySQLiteDemo.this.mtab.delete (73)

}

}

}

This is the end of the content of "how to use ContentValues to operate the database". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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