android两种方法操作Sqlite数据库

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/insertButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="插入" />

        <EditText
            android:id="@+id/insertEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </LinearLayout>

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除" />

        <EditText
            android:id="@+id/deleteEditText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

    </LinearLayout>
    
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/updateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改" />

        <EditText
            android:id="@+id/oldEditText"
            android:layout_width="119dp"
            android:layout_height="wrap_content"
            android:ems="10" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="改为" />

        <EditText
            android:id="@+id/newEditText"
            android:layout_width="92dp"
            android:layout_height="wrap_content"
            android:ems="10" />

    </LinearLayout>

   

    <Button
        android:id="@+id/selectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" />

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结果" />

</LinearLayout>
</pre><pre name="code" class="java"><pre name="code" class="java">package com.example.sqlite_2;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button insertButton, updateButton, deleteButton, selectButton;
	private EditText insertEditText, oldEditText, newEditText, deleteEditText;
	private TextView resultTextView;
	private StringBuilder sb = new StringBuilder();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		insertButton = (Button) findViewById(R.id.insertButton);
		deleteButton = (Button) findViewById(R.id.deleteButton);
		updateButton = (Button) findViewById(R.id.updateButton);
		selectButton = (Button) findViewById(R.id.selectButton);

		insertEditText = (EditText) findViewById(R.id.insertEditText);
		oldEditText = (EditText) findViewById(R.id.oldEditText);
		newEditText = (EditText) findViewById(R.id.newEditText);
		deleteEditText = (EditText) findViewById(R.id.deleteEditText);

		resultTextView = (TextView) findViewById(R.id.resultTextView);

		// //增
		insertButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				insert();
			}
		});
		// 删

		deleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根

				Log.i("删除开始", "删除开始");
				delete();

				Log.i("删除结束", "删除结束");

			}
		});

		// //改

		updateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根

				// db.update(table, values, whereClause, whereArgs)
				update();

			}
		});

		// //查
		selectButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				if (getCount()>0) {

					select();

				} else {

					resultTextView.setText("结果为空");

				}

			}
		});

	}// /onCreate

	private void insert() {

		CityDBHelper dbHelper = new CityDBHelper(MainActivity.this,
				"city_db_3", null, 1);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		// ContentValues cv = new ContentValues();
		// cv.put("city", insertEditText.getText().toString());
		// db.insert("city_table", null, cv);
		// db.close();
		//
		// resultTextView.setText("");
		db.execSQL("insert into city_table(city) values(?)",
				new Object[] { insertEditText.getText().toString() });
	}

	private void delete() {

		CityDBHelper dbHelper = new CityDBHelper(MainActivity.this,
				"city_db_3", null, 1);

		SQLiteDatabase db = dbHelper.getReadableDatabase();
		// String whereClauses = "city=?";
		// Log.i(" deleteEditText.getText().toString()",
		// deleteEditText.getText()
		// .toString());
		//
		// String[] whereArgs = { deleteEditText.getText().toString() };
		// db.delete("city_table", whereClauses, whereArgs);
		db.execSQL("delete  from   city_table where  city= ? ",
				new Object[] { deleteEditText.getText().toString() });

	}

	private void update() {

		CityDBHelper dbHelper = new CityDBHelper(MainActivity.this,
				"city_db_3", null, 1);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		//
		// ContentValues cv = new ContentValues();
		// cv.put("city", newEditText.getText().toString());
		// String whereClauses = "city=?";
		// String[] whereArgs = { oldEditText.getText().toString() };
		// db.update("city_table", cv, whereClauses, whereArgs);
		db.execSQL("update  city_table  set city=? where city  =?",
				new Object[] { newEditText.getText().toString(),
						oldEditText.getText().toString() });
	}

	private void select() {

		CityDBHelper dbHelper = new CityDBHelper(MainActivity.this,
				"city_db_3", null, 1);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		// Cursor cursor = db.query("city_table", new String[] { "city" }, null,
		// null, null, null, null, null);
		// sb.delete(0, sb.length());
		// while (cursor.moveToNext()) {
		// sb.append("城市:" + cursor.getString(cursor.getColumnIndex("city"))
		// + "\n");
		// }
		// resultTextView.setText(sb.toString());
		// db.close();
		sb.delete(0, sb.length());
		Cursor cursor = db.rawQuery("select *  from   city_table ", null);
		while (cursor.moveToNext()) {
			sb.append("城市:" + cursor.getString(0)
					+ "\n");
		}
		resultTextView.setText(sb.toString());
	}

	// private boolean boolean_select() {
	//
	// CityDBHelper dbHelper = new CityDBHelper(MainActivity.this,
	// "city_db_3", null, 1);
	//
	// SQLiteDatabase db = dbHelper.getWritableDatabase();
	// Cursor cursor = db.query("city_table", new String[] { "city" }, null,
	// null, null, null, null, null);
	// if (cursor.getCount() > 0) {
	// db.close();
	// return true;
	//
	// }
	//
	// return false;
	//
	// }
	private int getCount() {
		int count = 0;
		CityDBHelper dbHelper = new CityDBHelper(MainActivity.this,
				"city_db_3", null, 1);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select  count(*) from   city_table  ",
				null);
		while (cursor.moveToNext()) {

			count = cursor.getInt(0);
			Log.i("count", String.valueOf(count));
		}

		return count;

	}

}


 
 
<pre name="code" class="java">package com.example.sqlite_2;

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

public class CityDBHelper  extends SQLiteOpenHelper  {

	private   final  static  int   VERSION=1;
	
	
	
	
	public CityDBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO 自动生成的构造函数存根
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO 自动生成的方法存根
		db.execSQL("create  table   city_table (city  varchar(50) )");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO 自动生成的方法存根
		db.execSQL("alter  table   city_table   add   extends_column  varchar(10)");
	}

}


 
 

注释掉的单独是一个方法,可用于增删改查

android两种方法操作Sqlite数据库_第1张图片

android两种方法操作Sqlite数据库_第2张图片

你可能感兴趣的:(android,数据库,sqlite)