Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use ContentValues to operate the database

Shulou Source: shulou.com Published: 2022-05-31 18:17:58 09月28日 Update

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!

Tags: Data database content datasheet more knowledge practicality achievement and then just name dilemma reality situation article case version user ribbon website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Redmi NVidia Shulou Technology