/** * 通过Mysql创建SQlite * $db 数据库 * $tbname 表名 * $is_images 需要保存的图片二进制数组 array("img1","img2","img3")形式,默认为false * return SQL的数组形式 */ function creat_sqlite($db, $tbname, $is_images = false){ $runsql1 = $db->query("SELECT * FROM `$tbname` limit 1"); $fields_rows = $runsql1->columnCount(); $tb_fields_info = array(); $tb_string = ""; for ($i = 0; $i < $fields_rows; $i++) { $dot = ($i > 0) ? "," : ""; $tb_meta = $runsql1->getColumnMeta($i); //如果存在数据且字段存在需要取消的字段中 if($is_images && in_array($tb_meta['name'], $is_images)){ continue; } $tb_string .= $dot.$tb_meta['name']; switch($tb_meta['native_type']){ case "VAR_STRING": $tb_string .= " "."varchar"; break; case "LONG": $tb_string .= " "."integer"; break; case "BLOB": $tb_string .= " "."text"; break; } if(isset($tb_meta['flags'][1]) && ($tb_meta['flags'][1] === "primary_key")){ $tb_string .= " "."PRIMARY KEY"; } if($tb_meta['flags'][0] === "not_null") { $tb_string .= " "."NOT NULL"; } } //创建数据的SQL语句 $creat_tbs['data'] = "CREATE TABLE IF NOT EXISTS data ($tb_string);"; //创建图片的SQL语句 if($is_images) { $tb_images = ''; foreach($is_images as $k => $v){ $di = ($k > 0) ? " , " : " "; $tb_images .= $di.$v." blob NOT NULL"; } $creat_tbs['images'] = "CREATE TABLE IF NOT EXISTS images ( id integer PRIMARY KEY NOT NULL, tid integer NOT NULL,$tb_images);"; } return $creat_tbs; }
demo生成的结果(没设置image)为(我将其转换成了json形式):
{"data":"CREATE TABLE IF NOT EXISTS data (id integer PRIMARY KEY NOT NULL,name varchar NOT NULL,nickname varchar NOT NULL,emails varchar NOT NULL,passwd varchar NOT NULL,status NOT NULL);"} |