• SQLite 初步测试


    package org.prothro;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class SQLiteTestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
             final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/db.db3", null);
             
             Button btn = (Button)findViewById(R.id.button1);
             btn.setOnClickListener(new OnClickListener() {
                final    EditText username = (EditText)findViewById(R.id.username);
                final    EditText password = (EditText)findViewById(R.id.password);
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    
                    try{
                        
                        db.execSQL("insert into user values('"+username.getText().toString()+"','"+password.getText().toString()+"')");
                        
                    }catch(SQLException e){
                        e.printStackTrace();
                        db.execSQL(
                                "create table user(username varchar(20),password varchar(20));"
                                );
                        db.execSQL("insert into user values('"+username+"','"+password+"')");
                    }
                    
                    //取出数据库中所有的数据
                    Cursor cursor = db.rawQuery("select * from user", null);
                    
                    while(cursor.moveToNext()){
                        
                        Toast.makeText(
                                SQLiteTestActivity.this, 
                                cursor.getString(cursor.getColumnIndex("username"))+"   "+cursor.getString(cursor.getColumnIndex("password")), 
                                1000).show();
                    }
                    
                }
            });
        }
    }
  • 相关阅读:
    mybatis中refid是什么意思
    spring源码之—Assert.notNull()
    MySQL数据库查询 concat 字段合并 身份证 名字手机号脱敏 case when等
    Java 8 引入的一个很有趣的特性是 Optional 类
    <if test="distinct">distinct </if> 这样写的问题
    bit类型数据,是1=false 还是0=false
    开发用的软件
    写代码的心得
    SQL Server数据库级别触发器
    python访问aws-S3服务
  • 原文地址:https://www.cnblogs.com/laoquans/p/3070682.html
Copyright © 2020-2023  润新知