在Android应用中使用SQLite数据库是一种常见的数据存储方式,下面是一个简单的demo演示如何在Android应用中使用SQLite数据库:
- 创建数据库帮助类 DBHelper:
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "demo.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addUser(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
db.insert(TABLE_NAME, null, values);
db.close();
}
public ArrayList<String> getAllUsers() {
ArrayList<String> usersList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
usersList.add(name);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return usersList;
}
}
- 在MainActivity中使用数据库帮助类:
public class MainActivity extends AppCompatActivity {
private EditText etName;
private Button btnAddUser, btnShowUsers;
private ListView lvUsers;
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DBHelper(this);
etName = findViewById(R.id.et_name);
btnAddUser = findViewById(R.id.btn_add_user);
btnShowUsers = findViewById(R.id.btn_show_users);
lvUsers = findViewById(R.id.lv_users);
btnAddUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etName.getText().toString();
dbHelper.addUser(name);
Toast.makeText(MainActivity.this, "User added successfully", Toast.LENGTH_SHORT).show();
}
});
btnShowUsers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<String> usersList = dbHelper.getAllUsers();
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, usersList);
lvUsers.setAdapter(adapter);
}
});
}
}
- 在布局文件 activity_main.xml 中添加以下代码:
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_add_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add User"/>
<Button
android:id="@+id/btn_show_users"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Users"/>
<ListView
android:id="@+id/lv_users"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这样就可以在Android应用中使用SQLite数据库对用户信息进行增删改查操作了。希望以上内容对您有所帮助。
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "demo.db";
private static final String TABLE_NAME = "demo_table";
private static final String COL_ID = "id";
private static final String COL_NAME = "name";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_NAME + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
long result = db.insert(TABLE_NAME, null, contentValues);
return result != -1;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}
public boolean updateData(String id, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_ID, id);
contentValues.put(COL_NAME, name);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
return true;
}
public Integer deleteData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
}
}
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/157183.html