• android之SQLite java程序员


    package com.example.sql;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.graphics.Color;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    	private TextView tv;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		DBHelper db=new DBHelper(this);
    		tv=(TextView)findViewById(R.id.tv);
    		tv.setBackgroundColor(Color.RED);
    		db.clear();
    		//db.insert();
    		//String str=db.query(0);
    		//System.out.println(str);
    		//db.insert();
    		db.insert1(3,"cmaker");
    		String str=db.query(0);
    		tv.setText(str);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    }
    class DBHelper extends SQLiteOpenHelper
    {
    	private static final String CREATE_TABLE_SQL="create table TempTb1(_id integer,name text)";
    	private SQLiteDatabase db;
    	DBHelper(Context context)
    	{
    		super(context,"test.db",null,2);
    	}
    	@Override
    	public void onCreate(SQLiteDatabase db)
    	{
    		db.execSQL(CREATE_TABLE_SQL);
    	}
    	@Override
    	public void onUpgrade(SQLiteDatabase db,int OldVersion,int NewVersion)
    	{
    	}
    	public void insert()
    	{
    		String sql="insert into TempTb1(_id,name)values(1,'amaker')";
    		getWritableDatabase().execSQL(sql);
    	}
    	public void insert1(int id,String name)
    	{
    		//String sql="insert into TempTb1(_id,name)values(2,'bmaker')";
    		//getWritableDatabase().execSQL(sql);
    		getWritableDatabase().execSQL("insert into TempTb1(_id,name)values(?,?)",new Object[]{id,name});
    	}
    	public String query(int index)
    	{
    		SQLiteDatabase db=this.getWritableDatabase();
    		Cursor cur=db.query("TempTb1",null,null,null,null,null,null);
    		if(cur.moveToFirst())//note
    		{
    			cur.move(index);
    			int id=cur.getInt(0);
    			String name=cur.getString(1);
    			String str=id+":"+name;
    			//System.out.println(id+":"+name);
    			db.close();//note
    			return str;
    		}
    		return "Fail to find any item";
    	}
    	public void clear()
    	{
    		SQLiteDatabase db=this.getWritableDatabase();
    		db.execSQL("delete from TempTb1");
    	}
    }
    
    

  • 相关阅读:
    [转载] python 计算字符串长度
    收藏好文章
    centos7安装部署kafka_2.13-2.4.1集群
    (转)zookeeper-3.5.5安装报错:找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
    (转)Marathon私有镜像仓库用户名和密码方式
    centos7安装最新版git
    最新版Harbor搭建(harbor-offline-installer-v1.10.1.tgz)(转)
    安装不可描述服务端socket.error: [Errno 99] Cannot assign requested address错误:
    Integer值判断是否相等问题
    LVS实现Kubernetes集群高可用
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215862.html
Copyright © 2020-2023  润新知