News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Mod Authors: SMF 2.0 Database Functions

Started by SleePy, February 21, 2008, 05:57:21 PM

Previous topic - Next topic

Akyhne

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:
QuoteKode: [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??


Fustrate

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

Akyhne

If you don't put anything, you get an error in your log.

Fustrate

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

Akyhne

Yeah I did that. I just don't know why I have to do it.

Fustrate

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

Akyhne

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.

Fustrate

As I said, sqlite and postgresql seem to need it in their queries when doing an insert.

From Subs-dq-sqlite:
// 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

X3mE

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



X3mE

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:

$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



OutofOrder

Quote from: SleePy on February 21, 2008, 05:57:21 PM
$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]

Dannii

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."

OutofOrder


Bl4ck.bt

#53
hi, can anyone help me to transform this:

// 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  :)

Yağız...

I'm not sure but try this:

         // 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');
   }

Bl4ck.bt

 :(

it saying me

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

at line 60:

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


Yağız...

You should add $smcFunc to the global line.

Bl4ck.bt

 :D great!!

perfectly working  :)

thank you very much  :D

Bl4ck.bt

 :( not working

he deleted me all the lines in the table


i think because the line

               ", __FILE__, __LINE__);

is missing in the translation


Dragooon

__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).

Advertisement: