[code]
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('data.db3');
//打开目录下的数据库表;
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$data = array(
array(
'name' => 'james' ,
'url' => 'http://www.xxxxx.com'
),
array(
'name' => 'Andy' ,
'url' => 'http://www.xxxxxxxx.com'
)
); $this->db->insert_batch('test', $data);
/*↑这一行内容,始终报错:Fatal error: Using $this when not in object context in */
//Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo "Records created successfully\n";
}
$db->close();
?>
[/code]
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('data.db3');
//打开目录下的数据库表;
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$data = array(
array(
'name' => 'james' ,
'url' => 'http://www.xxxxx.com'
),
array(
'name' => 'Andy' ,
'url' => 'http://www.xxxxxxxx.com'
)
); $this->db->insert_batch('test', $data);
/*↑这一行内容,始终报错:Fatal error: Using $this when not in object context in */
//Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo "Records created successfully\n";
}
$db->close();
?>
[/code]
/*开始事务*/
begin transaction;
insert into test (name,url) values('1','2');
insert into test (name,url) values('3','4');
insert into test (name,url) values('5','6');
/*万条记录结束*/
/*执行事务*/
commit transaction;
但是如何不通过遍历循环,直接将变量全部插入到数据库中呢?
我是新手,表达能力不太强.
就是
$data="(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),(1,2,3),".这种带有间隔符的字符串,如何
使用insert into Table (Column_1,Column_2,Column_3) value $data;这样的效果,调用$data变量,直接一次性全部插入?
如果部分字符内容格式不对,如4个值或1个值,请问又如何跳过错误,自动插入下一个值呢?
class MyDB extends SQLite3
{
function __construct()
{
$this->open("C:\Users\Desktop\data.db3");
//打开本地数据库文件.
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
//如果没有报错,返回值提示连接成功.
} /*=======================方法1===================*/
$stmt = $db->prepare("INSERT INTO foo (id, bar) VALUES (:name, :value)");
/*这里的占位符,能够调用变量实现批量插入么??*/
$name = 1;
$value = `asdasda`;
$stmt->execute();
/*==========================方法2===================*/
$as="James";
$ab="100";
$sql = "INSERT INTO test (name,score) VALUES ('$as','$ab')";
/*=============这里可以调用变量,=======*/
/*=============但是如果改成$as="(James,100),(Andy,120),(Bily,90)" ,然后INSERT INTO test (name,score) VALUES $as; 还是容易插入不了*/
$stmt->execute();
$db->exec($sql); /*===============================*/
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo "Records created successfully\n";
}
$db->close();
?>?>
您总不能让 sqlite 去做他不能做的事情吧
不是也是要用循环的吗?
那么,为什么不能在循环里插入呢?
$stmt = $db->prepare("INSERT INTO foo (id, bar) VALUES (?, ?)");
foreach($data as $v) $stmt->execute($v);如果你坚持要 $stmt = $db->prepare("INSERT INTO foo (id, bar) VALUES (:name, :value)");
那么就
foreach($data as $item) {
foreach($item as $k=>$v) $t[":$k"] = $v;
$stmt->execute($t);
}
版主,谢谢你的回复.
$data的变量值,并不是遍历数组取到的值..
是拆分数组变量$mogo得到的
mogo里的值是
==========
(A,B,C)
(A,B,C)
(A,B,C)
(A,B,C)
(A,B,C)
(A,B,C)
=======
拆分后
$data="(A,B,C),(A,B,C),(A,B,C),(A,B,C),(A,B,C),(A,B,C)"初衷是数组直接拆分成批量入库的格式.
实现以下语句"insert into table_name Values (values1,.values2,values3),(values4,.values5,values6),(values7,.values8,values9)
非要让 sqlite 不认得的格式去做事,也太霸道点了吧