我又回到了安卓的学习当中,忙来忙去终于忙的差不多有时间做自己的事情了,这感觉实在是太棒了!!本来想写android的控件以及他们的监视器的,但是我查了查android的手册,基本上都能查到,但是查有些功能就比较麻烦,比如EditText中的TextWatcher接口,一般查到的都是OnEditorActionListener接口。好了废话不多说,先割了他!!!!

------------------------咯咯---------------------咯咯------------------------------咯咯------------------------------

创建工程之后,建立一个包,主要是写SQLite的。

再来补充下背景,在这里,我在first这个包里写了两个类,这是因为我在弄两个activity的切换,不影响本实验,本实验是放在OtherActivity.java里面的进行的。

建立一个Sqlite.java的类,继承SQLiteOpenHelper。

public class Sqlite extends SQLiteOpenHelper{    private static final int VERSION = 1;        public Sqlite(Context context, String name, CursorFactory factory,            int version) {        super(context, name, factory, version);        // TODO 自动生成的构造函数存根    }    public Sqlite(Context context,String name){        this(context,name,VERSION);    }    public Sqlite(Context context,String name,int version){        this(context, name,null,version);    }    @Override    public void onCreate(SQLiteDatabase arg0) {        // TODO 自动生成的方法存根        System.out.println("create a Database");        //execSQL函数用于执行SQL语句        arg0.execSQL("create table user(id int,name varchar(20))");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        // TODO 自动生成的方法存根        System.out.println("update a Database");    }}

在OtherActivity.java中加入这个包,

import com.yuyidong.db.Sqlite;

这里是布局,前面的TextView和Button(Call)请大家无视掉

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical" >        <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Test"        android:textSize="30sp"                />    <Button        android:id="@+id/button_other"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Call"        />    <Button        android:id="@+id/button_create"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Create"        />    <Button        android:id="@+id/button_update"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Update"        />    <Button        android:id="@+id/button_insert"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Insert"        />    <Button        android:id="@+id/button_update_table"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Update_Table"        />    <Button        android:id="@+id/button_query"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Query"        />    </LinearLayout>

接下来是申明已经添加到监听器中。

public class OtherActivity extends Activity{    private TextView text;    private Button button;    private Button createButton;    private Button insertButton;    private Button updateButton;    private Button updateRecordButton;    private Button queryButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO 自动生成的方法存根        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_other);             Intent intenter = getIntent();        String value = intenter.getStringExtra("hello");                text = (TextView) findViewById(R.id.text);        text.setText(value);        button = (Button) findViewById(R.id.button_other);        buttonListener lisbtn = new buttonListener();        button.setOnClickListener(lisbtn);                createButton = (Button) findViewById(R.id.button_create);        buttonListener createbtn = new buttonListener();        createButton.setOnClickListener(createbtn);        updateButton = (Button) findViewById(R.id.button_update);        buttonListener updatebtn = new buttonListener();        updateButton.setOnClickListener(updatebtn);        insertButton = (Button) findViewById(R.id.button_insert);        buttonListener insertbtn = new buttonListener();        insertButton.setOnClickListener(insertbtn);        updateRecordButton = (Button) findViewById(R.id.button_update_table);        buttonListener updatetablebtn = new buttonListener();        updateRecordButton.setOnClickListener(updatetablebtn);        queryButton = (Button) findViewById(R.id.button_query);        buttonListener querybtn = new buttonListener();        queryButton.setOnClickListener(querybtn);            }

请大家继续无视掉Intent、text、button这三个对象。

接下来讲一讲Button的监听器里面发生的故事。红色的注释是主要的说明。

class buttonListener implements OnClickListener    {        private Button button_check;        private int version = 1;;        @Override        public void onClick(View v) {            // TODO 自动生成的方法存根            
//将View的对象v转换成Button的
            button_check = (Button) v;
           //请无视掉这里,这个是转跳到发短信的Activity的Button的操作            if(button_check==button)            {                Uri uri = Uri.parse("smsto:10086");                Intent intenter = new Intent(Intent.ACTION_SENDTO,uri);                intenter.putExtra("sms_body", "Test good!");                startActivity(intenter);            }
            //如果是按下的创建数据库的那个Button的话,执行             else if(button_check == createButton)            {
              //创建一个Sqlite对象 
                Sqlite dbHelper = new Sqlite(OtherActivity.this,"yyd_test_db");
              //只有调用了Sqlite对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库                SQLiteDatabase db = dbHelper.getReadableDatabase();
               //Toast显示调试                Toast.makeText(OtherActivity.this, "Create", Toast.LENGTH_SHORT).show();            }            else if(button_check == updateButton)            {
                //每次更新后,数据库版本加1                version++;                Sqlite dbHelper = new Sqlite(OtherActivity.this,"yyd_test_db",version);                SQLiteDatabase db = dbHelper.getReadableDatabase();                Toast.makeText(OtherActivity.this, "Update", Toast.LENGTH_SHORT).show();            }            else if(button_check == insertButton)            {                //生成ContentValues对象                  ContentValues values = new ContentValues();
                //想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致                values.put("id", 1);                values.put("name","zhangsan");                Sqlite dbHelper = new Sqlite(OtherActivity.this, "yyd_test_db", version);                SQLiteDatabase db = dbHelper.getWritableDatabase();
                //调用insert方法,就可以将数据插入到数据库当中                 db.insert("user", null, values);                Toast.makeText(OtherActivity.this, "Insert", Toast.LENGTH_SHORT).show();            }            else if(button_check == updateRecordButton)            {
                //得到一个可写的SQLiteDatabase对象                Sqlite dbHelper = new Sqlite(OtherActivity.this, "yyd_test_db", version);                SQLiteDatabase db = dbHelper.getWritableDatabase();                ContentValues values = new ContentValues();                values.put("name", "zhangsanfeng");

//第一个参数是要更新的表名、第二个参数是一个ContentValeus对象、第三个参数是where子句
db.update(
"user", values, "id=?", new String[]{"1"});
Toast.makeText(OtherActivity.
this, "Update_Table", Toast.LENGTH_SHORT).show();
}
else if(button_check == queryButton)
{
Sqlite dbHelper
= new Sqlite(OtherActivity.this,"yyd_test_db");
SQLiteDatabase db
= dbHelper.getReadableDatabase();
Cursor cursor
= db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);

//用cursor.moveToNext()判断是否还存在下一个,若存在返回真,不存在返回假。
while(cursor.moveToNext())
{
String name
= cursor.getString(cursor.getColumnIndex("name"));
System.out.println(
"Get--->" + name);
}
}
}

}

不仅可以写程序操作SQLite,还可以用shell操作SQLite数据库。

在App程序里面创建了数据库之后才有了databases,登进去数据库,还是与一般关系型数据库还是有差别的。

总结一下,简短不割,SQLite在App中不提倡使用,因为他有时候会无缘无故的报错,因为我在测试的时候就经常发生,第一次把程序烧进去的时候,可以N次创建数据库同哟个数据库不报错,这就不说了,第二次打开的时候,无论点哪个Button都会直接程序崩溃,包括Insert,第一次的时候就不会,而且还可以查到数据表里面的信息,第二次就不行了,query也query不起了。很蛋疼。

转载请注明出处:http://www.cnblogs.com/yydcdut/p/3651977.html

更多相关文章

  1. 类和 Json对象
  2. Android系统配置数据库注释(settings.db)
  3. Android中文API(144) —— JsonWriter
  4. Android之Handler用法总结
  5. android通过ksoap2对webservice的解析
  6. Android(安卓)View的介绍和使用
  7. Android中,把XML文件转换成Object对象的方法
  8. Android(安卓)中数据库查询方法 query() 中的 select
  9. [置顶] android orm映射框架(类似hibernate)基本使用

随机推荐

  1. 【DB笔试面试710】在Oracle中,用哪个参数
  2. 内网绘图服务,老板乐的笑出大金牙
  3. 【DB笔试面试687】在Oracle中,常用的10046
  4. 从源码层次理解Spring事务
  5. 【DB笔试面试683】在Oracle中,什么是ORA-0
  6. 【DB笔试面试684】在Oracle中,什么是DUAL
  7. 【死磕ibatis】SqlMapClient 基本操作示
  8. 【DB笔试面试694】在Oracle中,什么是orato
  9. 唬人的Redis多线程,也就那么回事
  10. 【DB笔试面试698】在Oracle中,如何查看某