首页 > 代码库 > 安卓中实现对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数据表的增删改查