Advertisement:

Author Topic: SMF SQLite3 Wrapper  (Read 35944 times)

Offline tinoest

  • Development Contributor
  • Jr. Member
  • *
  • Posts: 267
Re: SMF SQLite3 Wrapper
« Reply #20 on: August 17, 2012, 04:20:46 PM »
I am thinking of making a class so anybody that is on SQLite2 can use it to. I am going to make the SQLite3 class that is found on all newer PHP versions if it is not found, but need some help making the class.

Code: [Select]
if (!class_exists('SQLite3')) {
class SQLite3 {
var $handle;
function SQLite3($data) {
$this->handle = sqlite_open($data);
}
public function exec($data) {
return sqlite_exec($this->handle, $data);
}
public function querySingle($data, $type = false) {
if ($type == true) {$type = false;} else {$type = true;}
return sqlite_single_query($this->handle, $data, $type);
}
public function escapeString($data) {
return sqlite_escape_string($data);
}
public function lastErrorCode() {
return sqlite_last_error();
}
}
}

Some of the problems I am facing is finding the SQLite3 alternative like for sqlite_udf_encode_binary().

Just tested and it sort of works

Do you know that you can invoke the SQLite as an object like you can in SQLite3 ( well you don't have a choice in that function )

So you can SQLite->queryExec for example.

Methods are at the top , although they are all mentioned in the procedural counterparts.
http://www.php.net/manual/en/ref.sqlite.php

Offline nend

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,755
  • 2 deep n2 the code
    • sicommnend on GitHub
    • SIComm.us
Re: SMF SQLite3 Wrapper
« Reply #21 on: August 17, 2012, 06:20:33 PM »
Yeah, already knew that. When I first started missing with SQLite I used to use those statements. Then I moved to SQLite3. I don't think I will use PDO though, don't like how it is set up for SQLite.

Offline tinoest

  • Development Contributor
  • Jr. Member
  • *
  • Posts: 267
Re: SMF SQLite3 Wrapper
« Reply #22 on: August 17, 2012, 07:07:17 PM »
Its not actually the PDO , that's something entirely different.

Although I have to admit , I prefer the procedural way for DB connections over the objects.

You probably need to add a check to ensure $this->handle still exists in your quoted code btw , atleast that was a problem I had with the SQLite3 and SMF 2.0

Offline emanuele

  • SMF Super Hero
  • *******
  • Posts: 14,156
  • Gender: Male
  • THERE'S JUST ME
Re: SMF SQLite3 Wrapper
« Reply #23 on: August 18, 2012, 05:27:50 PM »
Although by default , if you are using sqlite_ then I would hazard a guess at you know something about databases as MySQL if by far the norm in the open source environment , especially with php.
There are two kind of people that could use SQLite:
1) those who know what they are doing,
2) those that don't even know what a database is.
There is also a third category: those that did a mistake while installing and didn't notice until later, but that is rather small group.

Considering the timing (well...yeah...I know), I think that for 2.1 we will be still stuck with SQLite 2.8. The idea of a mod or seems the way to for now, that way there will be enough time to test different cases and include the support in 2.2. :)

I feel support the two versions in parallel could be a nightmare the moment we decide to drop 2.8 in favour of 3.x...


Take a peek at what I'm doing! ;D



Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non รจ una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Offline juscheqin

  • Newbie
  • *
  • Posts: 1
Re: SMF SQLite3 Wrapper
« Reply #24 on: April 03, 2013, 09:45:49 AM »
FWIW: today did a fresh install of latest SMF 2.0.4 on PHP 5.3.22 using SQLite 3.7.3 and above wrapper with edit to Subs-Db-sqlite.php as in first post before running install.php.

As far as I can tell it all works flawlessly - may fall over it host upgrades PHP of course. Just for feedback and thanks very much for the wrapper (only SQLite available for free hosting).

Offline kaneye2869

  • Newbie
  • *
  • Posts: 1
Re: SMF SQLite3 Wrapper
« Reply #25 on: November 30, 2020, 08:55:17 AM »
Original code for non registrered users:
Code: [Select]
<?php
if (!defined('SMF'))
    die(
'Hacking attempt...');

if(!
defined('SQLITE_NUM')) define 'SQLITE_NUM' SQLITE3_NUM);

// Included by default from php 5.3.x 
if(phpversion() < '5.3.0') return;

if(!
class_exists("SQLite3")) return;

if(!
function_exists('sqlite_open')) {
  function 
sqlite_open($location,$mode,$error) {
    
// Error and Mode not used     
    
$dB = new SQLite3($location);
    return 
$dB;
  }
}

if(!
function_exists('sqlite_popen')) {
  function 
sqlite_popen($location,$mode,$error) {    
    
// Error and Mode not used     
    
$dB = new SQLite3($location);
    return 
$dB;
  }
}

if(!
function_exists('sqlite_query')) {
  function 
sqlite_query($query$dB $result FALSE , &$errmsg FALSE ) {
    global 
$db_name;

    
$created FALSE;
    if(!
is_object($dB)) { 
      if (
substr($db_name, -3) != '.db')
        
$db_name .= '.db';

      
$dB = new SQLite3($db_name);
      
$created TRUE;
    }    
    if(
stristr($query,'SELECT'/*|| stristr($query,'INSERT') || stristr($query,'UPDATE') */) {
      
$result $dB->query($query);
    } else {  
      
$result $dB->exec($query); 
    }

    if(
$created === TRUE
      
$dB->close();

    if(
$result === FALSE
      return 
FALSE;
    
    return 
$result;
  }
}

if(!
function_exists('sqlite_fetch_array')) {
  function 
sqlite_fetch_array(&$result,$type SQLITE3_BOTH ) {
    if(!
is_object($result)) {
      return 
$result;
    }
    
$resx $result->fetchArray$type );
    return 
$resx;
  }
}

if(!
function_exists('sqlite_create_function')) {
  function 
sqlite_create_function $dB $funcname $callback $args '-1' ) {
    
$ret $dB->createFunction $funcname $callback $args);
    return 
$ret;
  }
}

if(!
function_exists('sqlite_escape_string')) {
  function 
sqlite_escape_string $query ) {
    global 
$db_connection $db_name;

    
$created FALSE;
    if(!
is_object($db_connection)) { 
      if (
substr($db_name, -3) != '.db')
        
$db_name .= '.db';

      
$dB = new SQLite3($db_name);
      
$created TRUE;
    } else {
      
$dB $db_connection;
    }

    
$esql $dB->escapeString($query);

    if(
$created === TRUE
      
$dB->close();

    return 
$esql;
  }
}

if(!
function_exists('sqlite_changes')) {
  function 
sqlite_changes () {
    global 
$db_connection $db_name;

    
$created FALSE;
    if(!
is_object($db_connection)) { 
      if (
substr($db_name, -3) != '.db')
        
$db_name .= '.db';

      
$dB = new SQLite3($db_name);
      
$created TRUE;
    } else {
      
$dB $db_connection;
    }
    
    
$changes $dB->changes();
    if(
$created === TRUE
      
$dB->close();
    

    return 
$changes;
  }
}

if(!
function_exists('sqlite_num_rows')) {
  function 
sqlite_num_rows $result ) {
    
$numRows 0
    while (
$result->fetchArray()) 
      
$numRows ++; 
    return (
$numRows); 

  }
}

if(!
function_exists('sqlite_libversion')) {
  function 
sqlite_libversion () {
    global 
$db_connection $db_name;

    
$created FALSE;
    if(!
is_object($db_connection)) { 
      if (
substr($db_name, -3) != '.db')
        
$db_name .= '.db';

      
$dB = new SQLite3($db_name);
      
$created TRUE;
    } else {
      
$dB $db_connection;
    }
    
    
$version $dB->version();
    if(
$created === TRUE
      
$dB->close();

    return 
$version;
  }
}

if(!
function_exists('sqlite_last_insert_rowid' )) {
  function 
sqlite_last_insert_rowid $dB ) {
    return 
$dB->lastInsertRowID();
  }
}

if(!
function_exists('sqlite_last_error')) {
  function 
sqlite_last_error $dB ) {
    global 
$db_name;

    
$created FALSE;
    if(!
is_object($dB)) {
      if (
substr($db_name, -3) != '.db')
        
$db_name .= '.db';

      
$dB = new SQLite3($db_name);
      
$created TRUE;
    }
    
    
$errCode $dB->lastErrorCode();

    if(
$created === TRUE)
      
$dB->close();

    return 
$errCode;
  }
}

if(!
function_exists('sqlite_error_string')) {
  function 
sqlite_error_string ($errno) {    
    global 
$db_connection $db_name;

    
$created FALSE;
    if(!
is_object($db_connection)) {
      if (
substr($db_name, -3) != '.db')
        
$db_name .= '.db';

      
$dB = new SQLite3($db_name);
      
$created TRUE;
    } else {
      
$dB $db_connection;
    }
    
    
$lastError $dB->lastErrorMsg();

    if(
$created === TRUE)
      
$dB->close();

    return 
$lastError;
  }
}

?>