SQLite

增删改查

增(insert into (表名)(参数列表) values(?,?,…,?)

public void click(View view) {
//        ContentValues contentValues = new ContentValues();
//        contentValues.put("name","大美");
//        db.insert("users",null,contentValues);

        String sql = "insert into users(name) values(?)";
        db.execSQL(sql,new Object[]{"卤蛋"});

    }

删(delete from (表名) where 条件语句)

 public void click(View view) {
//        ContentValues contentValues = new ContentValues();
//        contentValues.put("name","大美");
//        db.insert("users",null,contentValues);

      String sql = "insert into users(name) values(?)";
      db.execSQL(sql,new Object[]{"卤蛋"});

  }

改(update 表名 set 字段 = 值 where 条件语句)

 public void update(View view) {

//        String sql = "update users set name = ? where id = ?";
//        db.execSQL(sql,new Object[]{"高露洁",2});

     ContentValues contentValues = new ContentValues();
     contentValues.put("name","黑人");
     db.update("users",contentValues,"id=?",new String[]{"1"});

 }

查(select * from 表名 where 条件语句)

public void slect(View view) {

//        Cursor cursor = db.query("users", null, null, null, null, null, null);
//
//        if (cursor != null) {
//            while (cursor.moveToNext()) {
//                String name = cursor.getString(cursor.getColumnIndex("name"));
//                int id = cursor.getInt(cursor.getColumnIndex("id"));
//                Toast.makeText(this, name + "***" + id, Toast.LENGTH_SHORT).show();
//                Log.i(TAG, "slect: "+ id + "---" + name);
//            }
//        }
//        cursor.close();

        String sql = "select * from users";
        Cursor cursor = db.rawQuery(sql, new String[]{});
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                Toast.makeText(this, name + "***" + id, Toast.LENGTH_SHORT).show();
                Log.i(TAG, "slect: "+ id + "---" + name);
            }
        }
        cursor.close();

//        String sql = "select * from users";
//        db.execSQL(sql);
    }

SQLiteOpenHelper

package com.example.day1014.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqlHelp extends SQLiteOpenHelper {

    public MySqlHelp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /**
     * Called when the database is created for the first time. This is where the
     * creation of tables and the initial population of the tables should happen.
     *
     * @param db The database.
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table users(id integer primary key autoincrement,name varchar(20))");

        db.beginTransaction();
        for (int i = 0; i < 1000; i++) {

            db.execSQL("insert into users values(null,'大板牙')");
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    /**
     * Called when the database needs to be upgraded. The implementation
     * should use this method to drop tables, add tables, or do anything else it
     * needs to upgrade to the new schema version.
     *
     * <p>
     * The SQLite ALTER TABLE documentation can be found
     * <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
     * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
     * you can use ALTER TABLE to rename the old table, then create the new table and then
     * populate the new table with the contents of the old table.
     * </p><p>
     * This method executes within a transaction.  If an exception is thrown, all changes
     * will automatically be rolled back.
     * </p>
     *
     * @param db         The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值