I got a table with 4 fields:
id, int(11), auto increament email, varchar(32) pass, varchar(32)
date_created, date
My question is how my query should look like?
I mean I don't need to insert the first value to id because it's auto increment but I have to insert all of the values..
First of all, I hope you're using PreparedStatements.
Assuming you have a Connection object named conn and two strings email and password...
PreparedStatement stmt = conn.prepareStatement("INSERT INTO table_name(email, pass, date_created) VALUES (?, ?, ?)");
stmt.setString(1, email);
stmt.setString(2, password);
stmt.setDate(3, new Date());
stmt.executeUpdate();
In SQL you can specify which columns you want to set in the INSERT statement:
INSERT INTO table_name(email, pass, date_created) VALUES(?, ?, ?)
You can insert in the format
INSERT INTO YourTable (Your Columns) VALUES (Your Values)
So for e.g.
INSERT INTO Test_Table (email, pass, data_created) VALUES ('john#blah.com', 'pass', to_date(string, format))
Using parameters-tsql; (better to pass values in parameters rather than as strings)
Insert into [YourTableName] (email, pass, date_created)
values (#email, #pass, #date_created)
Related
I have a JDBC application that uses postgres INSERT INTO / ON CONFLICT to perform an upsert query. We would like to support H2 as well, but H2 uses the MERGE query instead. How would I go about translating the following query into H2's syntax? The H2 code will be in a separate library, which means that it doesn't have to work in both postgres and H2.
INSERT INTO dest_table(id,
field1,
field2,
field3,
field4)
VALUES (?, ?, ?, ?, ?)
on conflict (id) do update
set (field3,field3) = (excluded.field3, excluded.field4)
Of course, as soon as I posted to StackOverflow I figured it out. Here is my solution:
Table setup
drop table if exists characters;
CREATE TABLE characters (
id int not null,
first character varying(50),
last character varying(50));
insert into characters (id, first, last) values ( 1, 'scooby', 'doo' );
insert into characters (id, first, last) values ( 2, 'eric', 'cartman' );
insert into characters (id, first, last) values ( 3, 'luke', 'skywalker' );
select * from characters;
-------------------------
1,scooby,doo
2,eric,cartman
3,luke,skywalker
The update query looks like this (with hardcoded values, but it should work with placeholders as well)
Insert (no conflict):
merge into characters
using values (4, 'darth', 'vader') as incoming(id,first,last)
on characters.id = incoming.id
when not matched then
insert (id, first, last) values (incoming.id, incoming.first, incoming.last)
when matched then
update set first = incoming.first, last = incoming.last;
select * from characters;
-------------------------
1,scooby,doo
2,eric,cartman
3,luke,skywalker
4,darth,vader
Update (conflict on 'id', just updates darth vader to anikan skywalker):
merge into characters
using values (4, 'anikan', 'skywalker') as incoming(id,first,last)
on characters.id = incoming.id
when not matched then
insert (id, first, last) values (incoming.id, incoming.first, incoming.last)
when matched then
update set first = incoming.first, last = incoming.last;
select * from characters;
-------------------------
1,scooby,doo
2,eric,cartman
3,luke,skywalker
4,anikan,skywalker
I use Postgres and I've integration app which write data to database. My column should not be null but my app send null value. I tried to set default value but query override this rule with null value. How can i handle this change without code.
My Column configuration looks like this.
If you won't or can't change the query in code, you have to use trigger
If you can change code structure and query:
If the column has a default value, then no need to send NULL value to query
-- Before change
insert into your_table (id, name, default_col) values
(1, 'name', null);
-- After change (remove null data)
insert into your_table (id, name) values
(1, 'name');
Or send default value in insert query
-- Before change
insert into your_table (id, name, default_col) values
(1, 'name', null);
-- After change (Use default keyboard)
insert into your_table (id, name, default_col) values
(1, 'name', default);
I have been trying to insert data into the database, but it's continuously showing me this error, even though I not writing the name of the column(MemID) in the first fields, rather I am trying to enter it separately like this
create table Member (
MemID VARCHAR(20) PRIMARY KEY,
FullName VARCHAR(50),
email VARCHAR(50),
gender VARCHAR(50),
Contact_Number VARCHAR(50),
Registration_Number VARCHAR(50),
User_S# INT
);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Cleo Glynn', 'cglynncg#i2i.jp',
'Male', '81-(694)548-5205', '5443114970343516', 450);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Ivonne Deetlefs',
'ideetlefsch#virginia.edu', 'Female', '86-(257)683-5628',
'3571675846170605', 451);
insert into Member(MemID)
values('Mem0');
insert into Member(MemID)
values('Mem1');
insert into Member(MemID)
values('Mem2');
This code is giving me the error
Cannot insert the value NULL into column 'MemID', table
'master.dbo.Member'; column does not allow nulls. INSERT fails.
But I am inserting MemID column separately.
And also the table which has created above is not showing in the database, is it because of the error or something else?
That's not how inserts work.
When an insert is triggered sql tries to add the row immediately, without waiting for the consequent statements (it's stands true for any dml statements not only insert).
So when your first insert is triggered then sql is trying to put null in MemID as you haven't supplied any MemID in your insert.
Try inserting it in the very first place itself in the insert. Instead of separate statement something like:
insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Mem0','Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
EDIT
With response to your comment , I think Prepared statements is what you're looking for.
Since you're using python you could do prepare your insert statement like:
sql = "insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values (%s,'Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);"
sql = sql.format(self.db_scan_table)
//whenever you've memid ready use.
self.cursor.execute(sql, ('Mem01'))
Define an AUTO_INCREMENT column to generate ID values for you:
CREATE TABLE Member (
MemID INT AUTO_INCREMENT PRIMARY KEY,
-- ...
);
If this isn't practical because you need some kind of particular string definition for these then your only option is to supply it as part of the initial INSERT call. You have a constraint here, the PRIMARY KEY value must not be NULL.
should be able to using the query below. identify the next number
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member)+1 as varchar(100)),... )
if you want to start with 1 then + 1
otherwise remove + 1
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member) as varchar(100)),... )
How can I insert multiple values in 2 tables? I thought something like this, but that obviously doesn't work:
insert into login as l,
klantGegevens as k
(l.password,
l.rechten,
l.status,
k.voornaam,
k.achternaam,
k.woonplaats,
k.postcode,
k.telefoonnr)
values
('test',
1,
1,
'niels',
'jansen',
'Amsterdam',
'5993hk',
0623232323)
EDIT:
string intoDatabase = "insert into login (password,rechten,status) values(#password,#rechten,#status) insert into klantGegevens (voornaam,achternaam,woonplaats,postcode,telefoonnr) values(#voornaam,#achternaam,#woonplaats,#postcode,#telefoonnr)";
Only in separate commands :
insert into login (password,rechten,status)
values('test',1,1)
insert into klantGegevens(voornaam,achternaam,woonplaats,postcode,telefoonnr)
values('niels','jansen','Amsterdam','5993hk',0623232323)
I'm mediocre with perl and new to SQL, so excuse any lameness.
I've got a perl script I'm working on that interacts with a database to keep track of users on IRC. From tutorials I've found, I've been able to create the db, create a table within, INSERT a new record, UPDATE a field in that record, and SELECT/find records based on a field.
The problem is, I can only figure out how to UPDATE one field at a time and that seems inefficient.
The code to create the table:
my $sql = <<'END_SQL';
CREATE TABLE seenDB (
id INTEGER PRIMARY KEY,
date VARCHAR(10),
time VARCHAR(8),
nick VARCHAR(30) UNIQUE NOT NULL,
rawnick VARCHAR(100),
channel VARCHAR(32),
action VARCHAR(20),
message VARCHAR(380)
)
END_SQL
$dbh->do($sql);
And to insert a record using values I've determined elsewhere:
$dbh->do('INSERT INTO seenDB (nick, rawnick, channel, action, message, date, time) VALUES (?, ?, ?, ?, ?, ?, ?)', undef, $nickString, $rawnickString, $channelString, $actionString, $messageString, $dateString, $timeString);
So, when the script needs to update, I'd like to update all of these fiends at once, but right now the only thing that works is one at a time, using syntax I got from the tutorial:
$dbh->do('UPDATE seenDB SET time = ? WHERE nick = ?',
undef,
$timeString,
$nickString);
I've tried the following syntaxes for multiple fields, but they fail:
$dbh->do('UPDATE seenDB (rawnick, channel, action, message, date, time) VALUES (?, ?, ?, ?, ?, ?)', undef, $rawnickString, $channelString, $actionString, $messageString, $dateString, $timeString);
and
$dbh->do('UPDATE seenDB SET rawnick=$rawnickString channel=$channelString action=$actionString message=$messageString date=$dateString time=$timeString WHERE nick=$nickString');
Is there a better way to do this?
You can update several fields at once in pretty much the same way as you update a single field, just list them comma separated in the update, something like;
$dbh->do('UPDATE seenDB SET rawnick=?, channel=?, action=?, message=?, date=?, time=? WHERE nick=?',
undef,
$rawnickString,
$channelString,
$actionString,
$messageString,
$dateString,
$timeString,
$nickString
);