首页 > 代码库 > 安卓中实现对SQLITE数据表的增删改查
安卓中实现对SQLITE数据表的增删改查
直接上代码
MainActiviy.java
package com.example.sqllitcache; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { ListView lv; Button submit; EditText et; SQLiteDatabase dbwrite ; Dbhelper dbhelper; ArrayAdapter <String> adapter; Cursor c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) findViewById(R.id.listView1); submit=(Button) findViewById(R.id.button1); et=(EditText) findViewById(R.id.autoCompleteTextView1); adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); lv.setAdapter(adapter); dbhelper=new Dbhelper(this); dbwrite=dbhelper.getWritableDatabase(); getData(); //点击提交 submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String newData=http://www.mamicode.com/et.getText().toString();>Dbhelper.java
package com.example.sqllitcache; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class Dbhelper extends SQLiteOpenHelper { public Dbhelper(Context context ) { super(context, "mydb",null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("CREATE TABLE datatable ("+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+ "data String DEFAULT \"\")" ); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } }安卓中实现对SQLITE数据表的增删改查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。