zoukankan      html  css  js  c++  java
  • 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();
                    }
                    
                }
            });
        }
    }
  • 相关阅读:
    Docker 基础 B站 学习 最强 教程
    apache+php安装
    php拓展 swoole 安装
    go beego框架 入门使用 (一)
    php 使用 phpword 操作 word 读取 word
    linux + MongoDB 安装 + 部署 + 讲解 (满满干货看完记得收藏噢)
    Thanos设计简析
    Prometheus TSDB文件格式-index
    Linux Kernel文件系统写I/O流程代码分析(二)bdi_writeback
    Linux Kernel文件系统写I/O流程代码分析(一)
  • 原文地址:https://www.cnblogs.com/laoquans/p/3070682.html
Copyright © 2011-2022 走看看