0

I thought this would be easy, but I can't make it work:

Table=(int,int,text)

NpgsqlCommand command = new NpgsqlCommand("select * from database_data ncd inner join players p on p.player_id = ncd.player_id where p.playername ='Name1', conn);

string _data = (String)command.ExecuteScalar();

now I got the data....NOW I just wanna write it back...but...

NpgsqlCommand command2 = new NpgsqlCommand("Update database_data ncd inner join players p on p.player_id = ncd.player_id where p.playername ='Name2' + "'", conn);
command2.Parameters.Add(?????)
command2.ExecuteNonQuery();

I only managed to update the whole row.

NpgsqlCommand command2 = new NpgsqlCommand("insert into database_data values(1,2222,3333)", conn);
command2.ExecuteNonQuery();
conn.Close();

This works nicely, but I cannot combine it with the join and where.

If join doesn't work, I can simply work with where player_id=x and get the player_id somewhere else.

2 Answers 2

1

From my best knowledge update statement in pqsql should look like

update <table> set field1=value1, field2=value2 .... from FROM_CLAUSE WHERE WHERE_CLAUSE

But in your piece of code your statement did not match with this template. So, could you please tell more precisely what you want to do with data?

Sign up to request clarification or add additional context in comments.

3 Comments

havn't checked the answers yet....my intent is to write a new value into the field..I see I messed up the copy and paste in the example...I will fix that first
So I edited the question...the first Readstuff works...I just wanna do the same and write the data back....but when I add VALUE into the statement it doesn't work ....so again FIRST STRING works!!
"update database_data set data='teeeest' where player_id=17" this one works so far.....thx for the set....
0

This syntax is going to fail in Postgres unless you have a table called ncd.data:

Update ncd.data 
    from data ncd inner join
         players p
         on p.player_id = ncd.player_id
    where p.playername ='Name2' + "'";

I am guessing your intention is:

Update data ncd
    from players p
    where p.player_id = ncd.player_id and
          p.playername ='Name2' + "'";

5 Comments

yes I have a table database_data(edited it for better understanding) ...as said my first example works.... "update database_data set data='teeeest' where player_id=17"...this one works too....now I try and combine it with: ncd inner join players p on p.player_id = ncd.player_id where p.playername ='Name1' so I don't need to get the id in a seperate statement
just to make clear: this one work for reading: select * from database_data ncd inner join players p on p.player_id = ncd.player_id where p.playername ='Name1'
I just used two statements.... ID=select player_id from players where playername='Name' update database_data set data = '45645' where player_id =ID
@Tetragrammaton . . . Are you saying that the second query in this answer did not work? If so, I'll just delete the answer.
yes.....I JUST need to combine these two...just for curiosity: ++ID=select player_id from players where playername='Name'++ ++update database_data set data = '45645' where player_id =ID++ these two work in 2 "lines"...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.