Sqlite、以及系统常用的几个服务

前端之家收集整理的这篇文章主要介绍了Sqlite、以及系统常用的几个服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.imooc.sqlitedemo1;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 创建数据库、打开
		sqliteDatabase db = openOrCreateDatabase("user.db",MODE_PRIVATE,null);
		//执行数据库sql语句
		db.execsql("create table if not exists usertb (_id integer primary key autoincrement,name text not null,age integer not null,sex text not null )");
		db.execsql("insert into usertb(name,sex,age) values('张三','女',18)");
		db.execsql("insert into usertb(name,age) values('李四',19)");
		db.execsql("insert into usertb(name,age) values('王五','男',20)");

		//创建Cursor对象--执行查询方法,返回游标
		Cursor c = db.rawQuery("select * from usertb",null);
		if (c != null) {
			while (c.moveToNext()) {
				//取出数据
				Log.i("info","_id:" + c.getInt(c.getColumnIndex("_id")));
				Log.i("info","name:" + c.getString(c.getColumnIndex("name")));
				Log.i("info","age:" + c.getInt(c.getColumnIndex("age")));
				Log.i("info","sex:" + c.getString(c.getColumnIndex("sex")));
				Log.i("info","!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			}
			c.close();
		}
		db.close();
	}

}

************************************************二*******************************************************

package com.imooc.sqlitedemo2;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//创建并打开db
		sqliteDatabase db = openOrCreateDatabase("stu.db",null);
		//执行sql语句
		db.execsql("create table if not exists stutb(_id integer primary key autoincrement,sex text not null,age integer not null)");
		
		ContentValues values = new ContentValues();
		values.put("name","张三");
		values.put("sex","男");
		values.put("age",19);
		db.insert("stutb",null,values);
		values.clear();
		
		values.put("name","张三丰");
		values.put("sex",99);
		db.insert("stutb","张三疯");
		values.put("sex",59);
		db.insert("stutb","张三峰");
		values.put("sex",39);
		db.insert("stutb","张三封");
		values.put("sex",29);
		db.insert("stutb",values);
		values.clear();
		
		values.put("sex","女");
//		db.update(table,values,whereClause,whereArgs);
		db.update("stutb","_id>?",new String[]{"3"});//将全部id>3的人的性别改成女
//		db.delete(table,whereArgs);
		db.delete("stutb","name like ?",new String[]{"%丰%"});//删除所有名字中带有丰的人
		
//		db.query(table,columns,selection,selectionArgs,groupBy,having,orderBy)
		Cursor c = db.query("stutb",new String[]{"0"},"name");
		if (c!=null) {
			//取出所有列
			String [] columns= c.getColumnNames();
			while (c.moveToNext()) {
				for (String columnName : columns) {
					Log.i("info",c.getString(c.getColumnIndex(columnName)));
				}
			}
			c.close();
		}
		db.close();
	}

}

*************************************************************三************************************************
package com.imooc.sqlitedemo3;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DBOpenHelper helper =	new DBOpenHelper(MainActivity.this,"stu.db");
		sqliteDatabase db = helper.getWritableDatabase();
//		db.query(table,orderBy)
		Cursor c = db.rawQuery("select * from stutb",null);
		if (c!=null) {
			String [] cols = c.getColumnNames();
			while (c.moveToNext()) {
				for (String ColumnName : cols) {
					Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
				}
			}
			c.close();
		}
		db.close();
	}


}

package com.imooc.sqlitedemo3;

import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;

public class DBOpenHelper extends sqliteOpenHelper{
	
	public DBOpenHelper(Context context,String name) {
		super(context,name,1);
	}
	public DBOpenHelper(Context context,String name,CursorFactory factory,int version) {
		super(context,factory,version);
	}

	@Override//首次创建数据库的时候调用 一般可以把建库 建表的操作
	public void onCreate(sqliteDatabase db) {
		db.execsql("create table if not exists stutb(_id integer primary key autoincrement,age integer not null)");
		db.execsql("insert into stutb(name,age)values('张三',18)");
	}

	@Override//当数据库的版本发生变化的时候 会自动执行
	public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
		
	}

}

+++++++++++++++++++++++++++++++++++系统常用的几个服务********************************

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/network"
        android:onClick="doClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="判断网络是否连接" />

    <Button
        android:id="@+id/enableOrDisable_WIFI"
        android:layout_width="wrap_content"
         android:onClick="doClick"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/network"
        android:layout_alignRight="@+id/network"
        android:layout_below="@+id/network"
        android:text="开关WIFI" />

    <Button
        android:id="@+id/getvoice"
         android:onClick="doClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enableOrDisable_WIFI"
        android:layout_alignRight="@+id/enableOrDisable_WIFI"
        android:layout_below="@+id/enableOrDisable_WIFI"
        android:text="获取系统的音量" />

    <Button
        android:id="@+id/getPackagename"
                 android:onClick="doClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/getvoice"
        android:layout_alignRight="@+id/getvoice"
        android:layout_below="@+id/getvoice"
        android:text="获取当前进程包名" />

</RelativeLayout>

MainActivity
package comimooc.systemservicedemo;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
	// 判断网络是不是链接状态
	public boolean isNetWorkConnected(Context context) {
		if (context != null) {
			// 连接管理器
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(CONNECTIVITY_SERVICE);
			// 通过管理器,获取活跃的网络信息
			NetworkInfo mNetWorkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			// 如果网络信息不为空
			if (mNetWorkInfo != null) {
				return mNetWorkInfo.isAvailable();
			}
		}
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = (LayoutInflater) MainActivity.this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.activity_main,null);
		setContentView(view);
	}

	public void doClick(View v) {
		switch (v.getId()) {
		case R.id.network:
			if (isNetWorkConnected(MainActivity.this) == true) {
				Toast.makeText(MainActivity.this,"网络已经打开",Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(MainActivity.this,"网络未连接",Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case R.id.enableOrDisable_WIFI:

			WifiManager wifiManager = (WifiManager) MainActivity.this
					.getSystemService(WIFI_SERVICE);
			// 如果wifi可用,那么就关闭wifi
			if (wifiManager.isWifiEnabled()) {
				wifiManager.setWifiEnabled(false);
				Toast.makeText(MainActivity.this,"WIFI已经关闭",Toast.LENGTH_SHORT).show();
			} else {
				// 如果wifi不可用,那么就打开wify
				wifiManager.setWifiEnabled(true);
				Toast.makeText(MainActivity.this,"WIFI已经打开",Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.getvoice:
			// 音量管理器
			AudioManager mAudioManager = (AudioManager) MainActivity.this
					.getSystemService(AUdio_SERVICE);
			// 获取最大音量、当前音量
			int max = mAudioManager
					.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
			int current = mAudioManager
					.getStreamVolume(AudioManager.STREAM_RING);
			Toast.makeText(MainActivity.this,"系统的最大音量为:" + max + ",当前音量是:" + current,Toast.LENGTH_SHORT)
					.show();
			break;
		case R.id.getPackagename:
			ActivityManager activityManager = (ActivityManager) MainActivity.this
					.getSystemService(ACTIVITY_SERVICE);
			String packageName = activityManager.getRunningTasks(1).get(0).topActivity
					.getPackageName();
			Toast.makeText(MainActivity.this,"当前运行的Activity包名:" + packageName,Toast.LENGTH_SHORT).show();
			break;
		}
	}

}
原文链接:https://www.f2er.com/sqlite/199448.html

猜你在找的Sqlite相关文章