Insert hash into mysql

Hey!

I’m creating my own little database class, that I’m gonna use in some
school assignments. I can not use Activerecord for this :slight_smile:

I require the ‘mysql’ gem in my class.

I would like to create a method for inserting a row(s) in a specific
table. Here is my method:

def insert_row(table, params = {})
col = params.keys
result = params.values
@db.query(“INSERT INTO #{table} (#{col}) VALUES(#{result});”)
end

Almost as expected I get this error:

You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near ‘[:name,
:email]) VALUES([“Anders”, "[email protected]])’ at line 1.

So my question is. How can I parse a hash into mysql?

result.join(",")
turns ‘[“Anders”, "[email protected]]’ into ‘Anders,[email protected]

wich means you need that join for both col and result

Thanks, Hans!

When I do that, it creates a new error.

Unknown column ‘Anders’ in ‘field list’.

I think this error is due to the missing single-quotes around the
string. This creates a new problem: The single quotes should only be
there when its a string and not it its an integer or float.