一般做美食网站的产品需求外卖网站建设方案书
- 作者: 五速梦信息网
- 时间: 2026年04月20日 07:04
当前位置: 首页 > news >正文
一般做美食网站的产品需求,外卖网站建设方案书,站长聚集地,本溪做网站一、SQLiteDatabase SQLite 是一种轻量级的数据库引擎#xff0c;它非常适合在移动设备#xff08;例如#xff0c;Android#xff09;上使用 SQLiteDatabase 允许应用程序与 SQLite 数据库进行交互#xff0c;它提供了增删改查等一系列方法 二、SQLiteDatabase 简单编码…一、SQLiteDatabase SQLite 是一种轻量级的数据库引擎它非常适合在移动设备例如Android上使用 SQLiteDatabase 允许应用程序与 SQLite 数据库进行交互它提供了增删改查等一系列方法 二、SQLiteDatabase 简单编码
1、Application
MyApplication.javas
package com.my.database.application;import android.app.Application;
import android.content.Context;public class MyApplication extends Application {public static final String TAG MyApplication.class.getSimpleName();private static Context context;Overridepublic void onCreate() {super.onCreate();context this;}public static Context getContext() {return context;}
}2、Note
Note.java
package com.my.database.entity;public class Note {private int id;private String title;private String content;public Note(int id, String title, String content) {this.id id;this.title title;this.content content;}public int getId() {return id;}public void setId(int id) {this.id id;}public String getTitle() {return title;}public void setTitle(String title) {this.title title;}public String getContent() {return content;}public void setContent(String content) {this.content content;}Overridepublic String toString() {return Note{ id id , title title \ , content content \ };}
}3、Database
MyDatabaseManager.java
package com.my.database.mydatabase;import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;import com.my.database.application.MyApplication;
import com.my.database.entity.Note;import java.util.ArrayList;
import java.util.List;public class MyDatabaseManager {public static final String TAG MyDatabaseManager.class.getSimpleName();private SQLiteDatabase sqLiteDatabase;private static MyDatabaseManager myDatabaseManager;private static final String DATABASE_NAME test.db;private static final String TABLE_NAME Note;private static final String COLUMN_ID id;private static final String COLUMN_TITLE title;private static final String COLUMN_CONTENT content;private static final String CREATE_TABLE CREATE TABLE IF NOT EXISTS TABLE_NAME ( COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, COLUMN_TITLE TEXT, COLUMN_CONTENT TEXT);;private MyDatabaseManager() {sqLiteDatabase SQLiteDatabase.openOrCreateDatabase(MyApplication.getContext().getDatabasePath(DATABASE_NAME).toString(), null);sqLiteDatabase.execSQL(CREATE_TABLE);}public static MyDatabaseManager getInstance() {if (myDatabaseManager null) myDatabaseManager new MyDatabaseManager();return myDatabaseManager;}public void insert(Note note) {sqLiteDatabase.execSQL(INSERT INTO TABLE_NAME ( COLUMN_TITLE , COLUMN_CONTENT ) VALUES (?, ?);,new Object[]{note.getTitle(), note.getContent()});}public void delete(int id) {sqLiteDatabase.execSQL(DELETE FROM TABLE_NAME WHERE COLUMN_ID ?;, new Object[]{id});}public void update(Note note) {sqLiteDatabase.execSQL(UPDATE TABLE_NAME SET COLUMN_TITLE ?, COLUMN_CONTENT ? WHERE COLUMN_ID ?;,new Object[]{note.getTitle(), note.getContent(), note.getId()});}SuppressLint(Range)public ListNote queryAll() {Cursor cursor sqLiteDatabase.rawQuery(SELECT * FROM TABLE_NAME ;, null);if (cursor.moveToFirst()) {ListNote notes new ArrayList();do {Note note new Note(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)),cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));notes.add(note);} while (cursor.moveToNext());return notes;}return null;}
}4、Activity Layout
activity_note.xml
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.NoteActivityButtonandroid:idid/btn_insertandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text增app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /Buttonandroid:idid/btn_deleteandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text删app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/btn_insert /Buttonandroid:idid/btn_updateandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text改app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/btn_delete /Buttonandroid:idid/btn_query_allandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text查app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/btn_update /
/androidx.constraintlayout.widget.ConstraintLayout5、Activity Code
NoteActivity.java
package com.my.database;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.widget.Button;import com.my.database.entity.Note;
import com.my.database.mydatabase.MyDatabaseManager;import java.util.List;public class NoteActivity extends AppCompatActivity {public static final String TAG NoteActivity.class.getSimpleName();private MyDatabaseManager myDatabaseManager;private Button btnInsert;private Button btnDelete;private Button btnUpdate;private Button btnQueryAll;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_note);myDatabaseManager MyDatabaseManager.getInstance();btnInsert findViewById(R.id.btn_insert);btnDelete findViewById(R.id.btn_delete);btnUpdate findViewById(R.id.btn_update);btnQueryAll findViewById(R.id.btn_query_all);btnInsert.setOnClickListener(v - {Note note1 new Note(0, A, AAA);Note note2 new Note(0, B, BBB);myDatabaseManager.insert(note1);myDatabaseManager.insert(note2);Log.i(TAG, —————————— insert ok);});btnDelete.setOnClickListener(v - {myDatabaseManager.delete(1);myDatabaseManager.delete(2);Log.i(TAG, —————————— delete ok);});btnUpdate.setOnClickListener(v - {Note note1 new Note(0, A, AAA);Note note2 new Note(0, B, BBB);myDatabaseManager.update(note1);myDatabaseManager.update(note2);Log.i(TAG, —————————— update ok);});btnQueryAll.setOnClickListener(v - {ListNote notes myDatabaseManager.queryAll();if (notes null) {Log.i(TAG, —————————— queryAll - notes is null);return;}if (notes.size() 0) {Log.i(TAG, —————————— queryAll - notes is empty);return;}for (Note note : notes) Log.i(TAG, —————————— queryAll - note);});}
}Test
增 - 改 - 查 - 删 - 查输出结果
I/NoteActivity: —————————— insert ok
I/NoteActivity: —————————— update ok
I/NoteActivity: —————————— queryAll - Note{id1, titleA, contentAAA}
I/NoteActivity: —————————— queryAll - Note{id2, titleB, contentBBB}
I/NoteActivity: —————————— delete ok
I/NoteActivity: —————————— queryAll - notes is null三、SQLiteDatabase 简单编码案例解析
1、数据库结构
数据库文件名为 test.db
private static final String DATABASE_NAME test.db;数据库中有 Note 表它包含三个字段
private static final String TABLE_NAME Note;private static final String COLUMN_ID id;
private static final String COLUMN_TITLE title;
private static final String COLUMN_CONTENT content;字段类型备注id整型主键自增title文本-content文本-
2、单例模式
MyDatabaseManager 的构造函数是私有的防止外部代码直接创建实例对象
private MyDatabaseManager() {…
}MyDatabaseManager 实例对象只能通过 getInstance 方法获取该方法确保了整个应用程序中只有一个 MyDatabaseManager 实例对象
public static MyDatabaseManager getInstance() {if (myDatabaseManager null) myDatabaseManager new MyDatabaseManager();return myDatabaseManager;
}3、数据库创建
SQLiteDatabase.openOrCreateDatabase 方法用于打开或创建一个数据库如果数据库存在就是打开如果数据库不存在就是创建
sqLiteDatabase SQLiteDatabase.openOrCreateDatabase(MyApplication.getContext().getDatabasePath(DATABASE_NAME).toString(), null);execSQL 方法用于执行 SQL 语句
sqLiteDatabase.execSQL(CREATE_TABLE);4、数据库操作
1增 insert
接收一个 Note 对象并将其插入数据库
public void insert(Note note) {sqLiteDatabase.execSQL(INSERT INTO TABLE_NAME ( COLUMN_TITLE , COLUMN_CONTENT ) VALUES (?, ?);,new Object[]{note.getTitle(), note.getContent()});
}2删 delete
接收一个 id 来删除对应的记录
public void delete(int id) {sqLiteDatabase.execSQL(DELETE FROM TABLE_NAME WHERE COLUMN_ID ?;, new Object[]{id});
}3改 update
接收一个 Note 对象并根据 id 更新对应的记录
public void update(Note note) {sqLiteDatabase.execSQL(UPDATE TABLE_NAME SET COLUMN_TITLE ?, COLUMN_CONTENT ? WHERE COLUMN_ID ?;,new Object[]{note.getTitle(), note.getContent(), note.getId()});
}4查 queryAll
查询返回所有的行
SuppressLint(Range)
public ListNote queryAll() {Cursor cursor sqLiteDatabase.rawQuery(SELECT * FROM TABLE_NAME ;, null);if (cursor.moveToFirst()) {ListNote notes new ArrayList();do {Note note new Note(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)),cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));notes.add(note);} while (cursor.moveToNext());cursor.close();return notes;}cursor.close();return null;
}四、SQLiteDatabase 简单编码 SQL 语句
1、数据创建
CREATE TABLE IF NOT EXISTS Note (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,content TEXT
);// 写成一行CREATE TABLE IF NOT EXISTS Note (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT);2、数据库操作
1增 insert
INSERT INTO Note (title,content
) VALUES (【title】,【content】
);// 写成一行INSERT INTO Note (title, content) VALUES (【title】, 【content】);2删 delete
// 写成一行DELETE FROM Note WHERE id 【id】;3改 update
UPDATE Note SETtitle 【title】,content 【content】
WHERE id 【id】;// 写成一行UPDATE Note SET title 【title】, content 【content】 WHERE id 【id】;4查 queryAll
// 写成一行SELECT * FROM Note;注意事项 SQL 语句最好先在其他编辑器中写好如果直接在代码中编写极易写错 SQL 语句屑好后最好改写成一行之后粘贴到代码中然后调整结构否则结构会混乱
// 这是没写成一行的 SQL 语句private static final String CREATE_TABLE CREATE TABLE IF NOT EXISTS Note (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n title TEXT,\n content TEXT\n );;// 这写成一行的 SQL 语句粘贴到代码中然后调整结构的private static final String CREATE_TABLE CREATE TABLE IF NOT EXISTS Note ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT);;
- 上一篇: 一般自己怎么做网站做英文网站要会什么
- 下一篇: 一般做美食网站的产品需求中国有哪些企业
相关文章
-
一般自己怎么做网站做英文网站要会什么
一般自己怎么做网站做英文网站要会什么
- 技术栈
- 2026年04月20日
-
一般制作一个网站要多久个人网页图片模块制作
一般制作一个网站要多久个人网页图片模块制作
- 技术栈
- 2026年04月20日
-
一般网站自己可以做播放器吗优秀网页模板
一般网站自己可以做播放器吗优秀网页模板
- 技术栈
- 2026年04月20日
-
一般做美食网站的产品需求中国有哪些企业
一般做美食网站的产品需求中国有哪些企业
- 技术栈
- 2026年04月20日
-
一般做外单的有哪些网站手机做任务的网站有哪些内容
一般做外单的有哪些网站手机做任务的网站有哪些内容
- 技术栈
- 2026年04月20日
-
一般做网站哪家好网站页面建设需要ps吗
一般做网站哪家好网站页面建设需要ps吗
- 技术栈
- 2026年04月20日
