Advertisement:

Author Topic: Mod Authors: SMF 2.0 Database Functions  (Read 140744 times)

Offline Akyhne

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 7,331
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #40 on: December 18, 2008, 03:57:21 PM »
Quote
$smcFunc['db_insert'] (method, table, columns, data, keys, disable_trans, connection)
Emulated with smf_db_insert.
Method tells how to change the data. Accepts "replace" "ignore" or "insert".
Table: the data will be changed on.
Columns: An array ( column_name => input_type) set that holds all column names that will be changed and their expected input type.
Data holds an array that must be as long as the column array with all the data that will be used.
Keys is supposed to hold the tables key information, only appears to affect sqlite and postrgresql (when using "replace") versions.
Example:
Quote
Kode: [Vælg]
        $smcFunc['db_insert']('replace',
            '{db_prefix}log_topics',
            array(
                'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int',
            ),
            array(
                $user_info['id'], $topic, $modSettings['maxMsgID'],
            ),
            array('id_member', 'id_topic')
        );

"Keys is supposed to hold the tables key information, only appears to affect sqlite and postrgresql (when using "replace") versions."

So what does that mean??


Offline Fustrate

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 6,474
  • Gender: Male
  • Controller of the rum budget
    • Fustrate on GitHub
    • @Fustrate on Twitter
    • Fustrate
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #41 on: December 18, 2008, 08:27:27 PM »
IIRC, you can put an array of the table's keys there (i.e. array('id_member', 'id_board'))

I don't remember ever putting anything there, though... it's been a while since I've done any 2.0 coding.
Steven Hoffman
Former Team Member, 2009-2012

Offline Akyhne

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 7,331
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #42 on: December 18, 2008, 08:29:08 PM »
If you don't put anything, you get an error in your log.

Offline Fustrate

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 6,474
  • Gender: Male
  • Controller of the rum budget
    • Fustrate on GitHub
    • @Fustrate on Twitter
    • Fustrate
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #43 on: December 18, 2008, 08:36:44 PM »
Then you need to find the keys (index/primary) for the table you're modifying, and put them in an array in the "keys" parameter.
Steven Hoffman
Former Team Member, 2009-2012

Offline Akyhne

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 7,331
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #44 on: December 18, 2008, 09:18:44 PM »
Yeah I did that. I just don't know why I have to do it.

Offline Fustrate

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 6,474
  • Gender: Male
  • Controller of the rum budget
    • Fustrate on GitHub
    • @Fustrate on Twitter
    • Fustrate
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #45 on: December 18, 2008, 09:36:15 PM »
apparently sqlite and postgresql need it... and 2.0 is supposed to be compatible with those two also.
Steven Hoffman
Former Team Member, 2009-2012

Offline Akyhne

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 7,331
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #46 on: December 19, 2008, 07:16:15 AM »
Yeah I got that too, but if it is essential for a mod to work in SMF2 with other databases, I need to know what it is for.

Offline Fustrate

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 6,474
  • Gender: Male
  • Controller of the rum budget
    • Fustrate on GitHub
    • @Fustrate on Twitter
    • Fustrate
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #47 on: December 19, 2008, 10:46:48 AM »
As I said, sqlite and postgresql seem to need it in their queries when doing an insert.

From Subs-dq-sqlite:
Code: [Select]
// If it's a key we don't actally update it.
if (in_array($columnName, $keys))
$where .= (empty($where) ? '' : ' AND ') . substr($actualType,0, -2);
else
$updateData .= $actualType;
Steven Hoffman
Former Team Member, 2009-2012

Offline X3mE

  • Full Member
  • ***
  • Posts: 476
  • Gender: Male
  • The more I C, the less I see.
    • Lolmao.info - The site that makes you laugh!
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #48 on: January 24, 2009, 12:02:18 AM »
How can I set the character set to utf8 and the collation to utf8_general_ci with db_create_table?
Kids, you tried your best and you failed miserably. The lesson is - never try.

My mods:
OS & Browser Detection (1.5 is out!) | Skype Profile Field | GTalk Profile Field | AllCaps Blocker | SMF Syntax Highlighter (Beta) + 2 in development!

Personal websites:
Mobilize.rs (and forum) | Lolmao.info



Offline X3mE

  • Full Member
  • ***
  • Posts: 476
  • Gender: Male
  • The more I C, the less I see.
    • Lolmao.info - The site that makes you laugh!
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #49 on: January 24, 2009, 12:58:13 PM »
Is there any solution?

I'm currently doing several db_queries to change collations of the table and all columns after I create the table, like this:

Code: [Select]
$smcFunc['db_query']('',
'ALTER TABLE {db_prefix}table COLLATE utf8_general_ci');

$smcFunc['db_query']('',
'ALTER TABLE {db_prefix}table CHANGE colname colname type CHARACTER SET utf8 COLLATE utf8_unicode_ci');

And it works, but couldn't that be set in db_create_table somehow?
Kids, you tried your best and you failed miserably. The lesson is - never try.

My mods:
OS & Browser Detection (1.5 is out!) | Skype Profile Field | GTalk Profile Field | AllCaps Blocker | SMF Syntax Highlighter (Beta) + 2 in development!

Personal websites:
Mobilize.rs (and forum) | Lolmao.info



Offline OutofOrder

  • Jr. Member
  • **
  • Posts: 139
  • Please use the stairs
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #50 on: February 15, 2009, 07:14:27 AM »
$smcFunc['db_query'] (identifier, query, values, connection)
  • ...
  • Identifier is used for identifying specific queries that will be handled specially.
What is the identifier supposed to do/aim to? I want to know if i'm missing its functionality (which i could then take advantage of) simply because i'm ignoring what it does.
Do you have an example of the identifier in use?[/list]

Offline Dannii

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 10,212
  • Mind the volcano!
    • curiousdannii on GitHub
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #51 on: February 15, 2009, 08:12:20 AM »
You pretty much always leave the identifier blank. The only one I've ever used is truncate_table, which I think is needed to let it do text replacements with the different database systems.
"Never imagine yourself not to be otherwise than what it might appear to others that what you were or might have been was not otherwise than what you had been would have appeared to them to be otherwise."

Offline OutofOrder

  • Jr. Member
  • **
  • Posts: 139
  • Please use the stairs
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #52 on: February 20, 2009, 06:58:01 AM »
Allright, thanks :)

Offline Bl4ck.bt

  • Newbie
  • *
  • Posts: 8
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #53 on: March 17, 2009, 05:30:17 PM »
hi, can anyone help me to transform this:

Code: [Select]
// Delete it?
if (in_array($i, $delete_tags))
{
db_query("
DELETE FROM {$db_prefix}settings
WHERE variable LIKE 'custom123_%_$i'
", __FILE__, __LINE__);

$_POST['custom123_count']--;
}
else
{
$save_vars[] = array('check', 'custom123_enable_' . $i);
$save_vars[] = array('check', 'custom123_button_' . $i);
}
}

saveDBSettings($save_vars);

redirectexit('action=admin;area=featuresettings;sa=123');
}

is it equivalent to?

thanks  :)
« Last Edit: March 17, 2009, 05:32:07 PM by Bl4ck.bt »

Offline Yağız...

  • SMF Super Hero
  • *******
  • Posts: 10,811
  • Gender: Male
    • MMO Browser
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #54 on: March 17, 2009, 05:33:23 PM »
I'm not sure but try this:
Code: [Select]
         // Delete it?
         if (in_array($i, $delete_tags))
         {
            $smcFunc['db_query']('', '
               DELETE FROM {db_prefix}settings
               WHERE variable LIKE {text:123}',
   array(
'123' => 'custom123_%_$i',
   )
);

            $_POST['custom123_count']--;
         }
         else
         {
            $save_vars[] = array('check', 'custom123_enable_' . $i);
            $save_vars[] = array('check', 'custom123_button_' . $i);
         }
      }

      saveDBSettings($save_vars);

      redirectexit('action=admin;area=featuresettings;sa=123');
   }

Offline Bl4ck.bt

  • Newbie
  • *
  • Posts: 8
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #55 on: March 17, 2009, 05:54:23 PM »
 :(

it saying me

Fatal error: Function name must be a string in...

at line 60:

            $smcFunc['db_query']('', '


Offline Yağız...

  • SMF Super Hero
  • *******
  • Posts: 10,811
  • Gender: Male
    • MMO Browser
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #56 on: March 17, 2009, 06:00:22 PM »
You should add $smcFunc to the global line.

Offline Bl4ck.bt

  • Newbie
  • *
  • Posts: 8
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #57 on: March 17, 2009, 06:03:48 PM »
 :D great!!

perfectly working  :)

thank you very much  :D

Offline Bl4ck.bt

  • Newbie
  • *
  • Posts: 8
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #58 on: March 18, 2009, 05:55:42 AM »
 :( not working

he deleted me all the lines in the table


i think because the line

Code: [Select]
               ", __FILE__, __LINE__);
is missing in the translation


Offline Dragooon

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 6,738
  • Gender: Male
  • I'm bIn
    • ShitizGarg on Facebook
    • Dragooon on GitHub
    • dragooon on LinkedIn
    • SMF-Media
Re: Mod Authors: SMF 2.0 Database Functions
« Reply #59 on: March 18, 2009, 06:34:07 AM »
__FILE__,__LINE__ is not required anymore.

What do you mean? That code deleted all entries in the table? (The error I noticed is using {text:123} instead of {string:123} but I believe it should throw a fatal error on that).