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 placeholders to operate a database

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article will explain in detail how to use placeholders to operate the database. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Effect picture

In main.xml:

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

Package com.li.sqlite

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

String sql = "INSERT INTO" + TABLENAME + "(name,birthday) VALUES

Object args [] = new Object [] {name,birthday}

This.db.execSQL (sql,args)

This.db.close ()

}

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

String sql = "UPDATE" + TABLENAME + "SET name=?,birthday=? WHERE id=?"

Object args [] = new Object [] {name,birthday,id}

This.db.execSQL (sql,args)

This.db.close ()

}

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

String sql = "DELETE FROM" + TABLENAME + "WHERE id=?"

Object args [] = new Object [] {id}

This.db.execSQL (sql,args)

This.db.close ()

}

}

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 (30, "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 (31)

}

}

}

This is the end of the article on "how to use placeholders to operate the database". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

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