GaussDB增删改查操作(备忘)

加字段alter table tablename add (column1 types1,column2 types2)

插数据insert into…和别的一样,基本用不到
删数据delete from …和别的一样,基本用不到
改字段alter table tablename alter column columnname type types
改数据update tableName set column1=value1 where column2=value2
查数据select * from…

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的备忘录实现增删改查功能的代码,使用的是SQLite数据库: ``` public class MemoDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "memo.db"; private static final int DATABASE_VERSION = 1; public MemoDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_MEMO_TABLE = "CREATE TABLE " + MemoContract.MemoEntry.TABLE_NAME + "(" + MemoContract.MemoEntry._ID + " INTEGER PRIMARY KEY," + MemoContract.MemoEntry.COLUMN_TITLE + " TEXT," + MemoContract.MemoEntry.COLUMN_CONTENT + " TEXT," + MemoContract.MemoEntry.COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP" + ")"; db.execSQL(CREATE_MEMO_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + MemoContract.MemoEntry.TABLE_NAME); onCreate(db); } public long insertMemo(String title, String content) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(MemoContract.MemoEntry.COLUMN_TITLE, title); values.put(MemoContract.MemoEntry.COLUMN_CONTENT, content); long id = db.insert(MemoContract.MemoEntry.TABLE_NAME, null, values); db.close(); return id; } public List<Memo> getAllMemos() { List<Memo> memos = new ArrayList<>(); String selectQuery = "SELECT * FROM " + MemoContract.MemoEntry.TABLE_NAME + " ORDER BY " + MemoContract.MemoEntry.COLUMN_TIMESTAMP + " DESC"; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { Memo memo = new Memo(); memo.setId(cursor.getInt(cursor.getColumnIndex(MemoContract.MemoEntry._ID))); memo.setTitle(cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TITLE))); memo.setContent(cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_CONTENT))); memo.setTimestamp(cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TIMESTAMP))); memos.add(memo); } while (cursor.moveToNext()); } db.close(); return memos; } public Memo getMemoById(long id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(MemoContract.MemoEntry.TABLE_NAME, new String[]{MemoContract.MemoEntry._ID, MemoContract.MemoEntry.COLUMN_TITLE, MemoContract.MemoEntry.COLUMN_CONTENT, MemoContract.MemoEntry.COLUMN_TIMESTAMP}, MemoContract.MemoEntry._ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Memo memo = new Memo( cursor.getInt(cursor.getColumnIndex(MemoContract.MemoEntry._ID)), cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TITLE)), cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_CONTENT)), cursor.getString(cursor.getColumnIndex(MemoContract.MemoEntry.COLUMN_TIMESTAMP)) ); db.close(); return memo; } public int updateMemo(Memo memo) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(MemoContract.MemoEntry.COLUMN_TITLE, memo.getTitle()); values.put(MemoContract.MemoEntry.COLUMN_CONTENT, memo.getContent()); return db.update(MemoContract.MemoEntry.TABLE_NAME, values, MemoContract.MemoEntry._ID + " = ?", new String[]{String.valueOf(memo.getId())}); } public void deleteMemo(long id) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(MemoContract.MemoEntry.TABLE_NAME, MemoContract.MemoEntry._ID + " = ?", new String[]{String.valueOf(id)}); db.close(); } } ``` Memo.java: ``` public class Memo { private int id; private String title; private String content; private String timestamp; public Memo() { } public Memo(int id, String title, String content, String timestamp) { this.id = id; this.title = title; this.content = content; this.timestamp = timestamp; } 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; } public String getTimestamp() { return timestamp; } public void setTimestamp(String timestamp) { this.timestamp = timestamp; } } ``` MemoContract.java: ``` public final class MemoContract { private MemoContract() {} public static class MemoEntry implements BaseColumns { public static final String TABLE_NAME = "memo"; public static final String COLUMN_TITLE = "title"; public static final String COLUMN_CONTENT = "content"; public static final String COLUMN_TIMESTAMP = "timestamp"; } } ``` 然后在你的Activity或Fragment中,你可以如下使用: ``` public class MemoActivity extends AppCompatActivity { private MemoDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_memo); dbHelper = new MemoDatabaseHelper(this); // 插入备忘录 long memoId = dbHelper.insertMemo("Title", "Content"); // 获取所有备忘录 List<Memo> memos = dbHelper.getAllMemos(); // 获取单个备忘录 Memo memo = dbHelper.getMemoById(memoId); // 更新备忘录 memo.setTitle("New Title"); dbHelper.updateMemo(memo); // 删除备忘录 dbHelper.deleteMemo(memoId); } } ``` 当然,你还需要在你的布局文件中添加相应的UI元素,例如EditText、ListView等等,以显示备忘录的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值