package com.example.test13;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class DBActivity extends Activity{ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } protected void onCreat(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); String sname="chengkaige"; String sid="2014011797"; Button b1=(Button)this.findViewById(R.id.butCreateDB); b1.setOnClickListener(new OnClickListener(){ public void onClick(View v) { SQLiteDatabase db =SQLiteDatabase.openOrCreateDatabase("/scard/abc.db",null); }}); //建库 Button b2=(Button)this.findViewById(R.id.button1); b2.setOnClickListener(new OnClickListener(){ public void onClick(View v) { SQLiteDatabase db =SQLiteDatabase.openOrCreateDatabase("/scard/abc.db",null); String sql ="create table books(id VARCHAR(30) NOT NULL,price float"; db.execSQL(sql); }}); //建表,而且table里面一个库只能创建一次。 Button b3=(Button)this.findViewById(R.id.button2); b3.setOnClickListener(new OnClickListener(){ public void onClick(View v) { SQLiteDatabase db =SQLiteDatabase.openOrCreateDatabase("/scard/abc.db",null); String sname="chengkaige"; String sid="2014011797"; ContentValues newValues = new ContentValues(); newValues.put("id", sid); newValues.put("name", sname); newValues.put("price", 100); db.insert("books",null,newValues); }}); //插入 Button b4=(Button)this.findViewById(R.id.button3); b4.setOnClickListener(new OnClickListener(){ public void onClick(View v) { SQLiteDatabase db =SQLiteDatabase.openOrCreateDatabase("/scard/abc.db",null); db.delete("books", null, null); }}); //删除,delete不返回,books加双引号,因为是字符串。 Button b5=(Button)this.findViewById(R.id.button4); b5.setOnClickListener(new OnClickListener(){ public void onClick(View v) { SQLiteDatabase db =SQLiteDatabase.openOrCreateDatabase("/scard/abc.db",null); ContentValues updateValues = new ContentValues(); updateValues.put("name","sname"); updateValues.put("id", "sid"); }}); //更新功能。 Button b6=(Button)this.findViewById(R.id.button5); b6.setOnClickListener(new OnClickListener(){ public void onClick(View v) { String sname="chengkaige"; String sid="2014011797"; SQLiteDatabase db =SQLiteDatabase.openOrCreateDatabase("/scard/abc.db",null); Cursor m_cursor = db.query("books", null,"id='"+sid+"'", null, null, null, null); String strRes=""; while(m_cursor.moveToNext()){ String str1=m_cursor.getString(1); strRes=strRes+str1+"=="; } Toast.makeText(DBActivity.this, strRes, Toast.LENGTH_LONG); }}); //查询功能。 } }
目前暂无任何回答
- 0 回答
- 0 关注
- 973 浏览
添加回答
举报
0/150
提交
取消