每日总结2023/3/2
今日完成了本地数据库内容的按钮添加
代码如下
Button addData=(Button)findViewById(R.id.add_data); addData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SQLiteDatabase db=dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name","zhangsan"); values.put("number","20214153"); db.insert("student",null,values); values.clear(); } });
<Button android:id="@+id/add_data" android:layout_width="100dp" android:layout_height="100dp" android:text="添加" />
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; /** * Created by 86191 on 2023/3/1. */ public class MyDatabaseHelper extends SQLiteOpenHelper{ public static final String CREAT_Student="create table Student(" +"id interger ," +"name text," +"number text)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); mContext=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREAT_Student); Toast.makeText(mContext,"Creat succeeded",Toast.LENGTH_SHORT); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }