SQLite数据库操作

连接SQLite数据库并读取所有数据:


<?php

	if(extension_loaded('sqlite3')){
		// 支持SQLite3
	}elseif(extension_loaded('sqlite')){
		// 支持SQLite
	}else{
		// 不支持SQLite
	}


function db_create(){
	$db = new SQLite3('mogublog.db');
    $sql = array(
		"CREATE TABLE users ( uid INTEGER NOT NULL CHECK (uid>= 0) DEFAULT 0, name VARCHAR(60) NOT NULL DEFAULT '', pass VARCHAR(128) NOT NULL DEFAULT '', mail VARCHAR(254) NOT NULL DEFAULT '');",
		"CREATE TABLE users_status ( uid INTEGER NOT NULL DEFAULT 0, status INTEGER NOT NULL DEFAULT 0);"
	);
	$i = true;
	foreach($sql as $v){
		$result = $db->query($v);
		if(!$result){
			$i = false;
		}
	}
    if($i){
        echo 'Create Database Success!';
    }else{
        echo 'Create Database False';
    }
}

function db_insert(){
	$db = new SQLite3('mogublog.db');
	// $sql = "insert into users('name', 'pass', 'mail') values('admin', '123456', 'admin@mogu.cn')";
	$sql = "insert into users_status values('0', '1')";
	$result = $db->query($sql);
	var_dump($result);
}

function db_select(){
	$db = new SQLite3('mogublog.db'); 
    // $sql = "SELECT * FROM users"; 
    $sql = "SELECT * FROM users_status"; 
    $result = $db->query($sql);
    while($re = $result->fetchArray(SQLITE3_ASSOC)){
        $arr[] = $re;
    }
    var_dump($arr);
}

// db_insert();

db_select();

发表评论