update sql statement not working - sql-update

i have this code
earthid is the primary key and this is what i use to make sure the exact row is updated.
int row=jTable1.getSelectedRow();
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET year="+year1+", month="+month1+", day="+day1+", mjd="+mjd1+",ut1utcdelta="+Ut1utc+" WHERE (earthid="+row1+")";
yet when i execute this statement nothing happens....the database does not get updated with the new values
i cannot figure out whats wrong with my sql update statement
ok here is more of my code that is behind my update button.
jTable1.getModel().addTableModelListener(jTable1);
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
int row=jTable1.getSelectedRow();
int row1=row+1;
int column=0;
String years="";
String months="";
String days="";
String mjds="";
String ut1utc="";
int year1;
int month1;
int day1;
double mjd1;
double Ut1utc;
while(column!=7)
{
Object year=model.getValueAt(row, column);
years=year.toString();
year1=Integer.parseInt(years);
column++;
Object Month=model.getValueAt(row, column);
months=Month.toString();
month1=Integer.parseInt(months);
column++;
Object Day=model.getValueAt(row, column);
days=Day.toString();
day1=Integer.parseInt(days);
column++;
Object MJD=model.getValueAt(row, column);
mjds=MJD.toString();
mjd1=Double.parseDouble(mjds);
column++;
Object UT1UTCDELTA=model.getValueAt(row,column);
ut1utc=UT1UTCDELTA.toString();
Ut1utc=Double.parseDouble(ut1utc);
column++;
try
{
DBConnectionclass cm=new DBConnectionclass();
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET year="+year1+", month="+month1+", day="+day1+", mjd="+mjd1+",ut1utcdelta="+Ut1utc+" WHERE (earthorientationparameterid="+row1+")";
Connection con1=null;
Statement stmt=null;
con1=cm.getConnection(); //get a connection object
stmt=con1.createStatement();
stmt.executeUpdate(sql);
//create an instance of the database connection class to create a connection object to beused to conect to database
//create a statement to process sql query
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
// TOD
}
its not the best way to write code but it worked just fine with mysql. Do i need to do a select for update

Have you tried to use single quotes inside the SQL query? .I have edited your query give it a try
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET year='+year1+', month='+month1+', day='+day1+', mjd='+mjd1+',ut1utcdelta='+Ut1utc+' WHERE (earthid='+row1+')";
Also are you getting any exception?

Try putting single quotes around your values.
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER SET year='"+year1+"', month='"+month1+"', day='"+day1+"', mjd='"+mjd1+"',ut1utcdelta='"+Ut1utc+"' WHERE (earthid='"+row1+"')";

Your date parameters in the string should have single quotes around them. I assume earthid is an int and not a GUID as well, or that will need single quotes as well. Have you tried a parametrised query? Can you post a larger code sample?

hey guys i solved the problem. This is my fault totally. I when i select and return a record set from the database i did not explicitly specifiy which order it would come back in. Hence my record set was out of order and so it wasperforming an update but i it wasnt in order because my update explicitly updated by earthorientationparametersid. It had nothing to do with the update but with the order in which my grid displayed data and thats why i couldnt see an update

Related

Array Formula to Automatically Calculate

I want an array formula to use with Google Forms data to automatically calculate running metrics on my data.
In this case, in column BF I want it to auto-calculate whenever new responses come in.
I'm trying to use this formula below but isn't working for all column, just for the first line.
=ArrayFormula(If(isblank($BF$3:$BF);"";(INDEX($C$1:$BD$1;0;MATCH(MAX($C3:$BD3);$C3:$BD3;0)))))
What I'm doing wrong?
My Google Sheet:
https://docs.google.com/spreadsheets/d/1iD75djqdY8qtClEHpKUEvAS55bP0ARv9AnDUz9mFBuc/edit?usp=sharing
delete range BE:BF
paste this in BE1:
={"MAX"; ""; ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(C3:BD);
"select "&TEXTJOIN(","; 1; IF(A3:A<>"";
"max(Col"&ROW(A3:A)-ROW(A3)+1&")"; ))&""));
"select Col2"; 0))}
paste this in BF1:
={"TIPO"; ""; ARRAYFORMULA(IF(A3:A="";;TRIM(TRANSPOSE(QUERY(TRANSPOSE(
IF(C3:BD=QUERY(TRANSPOSE(QUERY(TRANSPOSE(C3:BD);
"select "&TEXTJOIN(","; 1; IF(A3:A<>"";
"max(Col"&ROW(A3:A)-ROW(A3)+1&")"; ))&""));
"select Col2"; 0); C1:1; ));;99^99)))))}

1-indexed Model in QCombobox

I have a 1-indexed (readonly-)model and want to use it for a combobox.
I parse the data (comes from a file-parser) and have for example:
1: Variable Number 1 and that will be my first item, next
2: Variable Number 2 and so on.
When I click on an item the currentIndex()-method from QCombobox will give me a 0-indexed int, so my problem is:
I don't want to write everytime I parse a file +1 respectively -1 when writing back to the file (although the model is readonly, I can alter the data in the file). (I have nearly 30 UIs where I need the model, and for every UI I have to parse other data)
I currently use something like:
virtual int currentIndex() const { return QComboBox::currentIndex() + 1; }
virtual void setCurrentIndex(int index) { QComboBox::setCurrentIndex(index-1); }
I know that this is not ideal, because (set-)currentIndex is not virtual. But to avoid +/-1 I used this for now.
Does anybode have a good suggestion for this problem?
If you have a custom model you could add a role that returns the "real" index value.
If you just use strings to fill the combobox, you could use the setItemData() and itemData() methods to associated your reference value.
E.g.
comboBox->addItem("Number 1", 1);
and
int refValue = comboBox->itemData(comboIndex).toInt();
The associated data can be anything that can be stored in a QVariant.

Insert JSON format in Mysql query using C++

I am using JSON format to save data in my c++ program , i want to send it to MySql database (the table tab has one column with type : TEXT) but the query failed (tested also VARCHAR and CHAR )
this is a part of the code since we are not interrested in the rest
string json_example = "{\"array\":[\"item1\",\"item2\"], \"not an array\": \"asdf\"}";
mysql_init(&mysql); //initialize database connection
string player="INSERT INTO tab values (\"";
player+= json_example;
player += "\")";
connection = mysql_real_connect(&mysql,HOST,USER,PASSWD,DB,0,NULL,0);
// save data to database
query_state=mysql_query(connection, player.c_str()); // use player.c_str()
to show the final query that will be used : cout << player gives :
INSERT INTO tab values ("{"array":["item1","item2"], "not an
array": "asdf"}")
using for example string json_example = "some text"; is working
but with the json format it is not working , maybe the problem came from the use of curly bracket {} or double quotes "" but i haven't find a way to solve it .
i'm using :
mysql Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (armv7l) under raspberry pi 2
Any help will be appreciated , thanks .
Use a prepared statement. See prepared statements documentation in the MySQL reference manual.
Prepared statements are more correct, safer, possibly faster, and keep your code cleaner. You get all those benefits and don't need to escape anything. There is hardly a reason not to use them.
Something like this might work. But take it with a grain of salt, because I have not tested or compiled it. It should just give you the general idea:
MYSQL_STMT* const statement = mysql_stmt_init(&mysql);
std::string const query = "INSERT INTO tab values(?)";
mysql_stmt_prepare(statement, query, query.size());
MYSQL_BIND bind[1] = {};
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = json_example.c_str();
bind[0].buffer_length = json_example.size();
mysql_stmt_bind_param(statement, bind);
mysql_stmt_execute(statement);

<Binary> in sql

I want to select all the binary data from a column of a SQL database (SQL Server Enterprise) using C++ query. I'm not sure what is in the binary data, and all it says is .
I tried this (it's been passed onto me to study off from) and I honestly don't 100% understand the code at some parts, as I commented):
SqlConnection^ cn = gcnew SqlConnection();
SqlCommand^ cmd;
SqlDataAdapter^ da;
DataTable^ dt;
cn->ConnectionString = "Server = localhost; Database=portable; User ID = glitch; Pwd = 1234";
cn->Open();
cmd=gcnew SqlCommand("SELECT BinaryColumn FROM RawData", cn);
da = gcnew SqlDataAdapter(cmd);
dt = gcnew DataTable("BinaryTemp"); //I'm confused about this piece of code, is it supposed to create a new table in the database or a temp one in the code?
da->Fill(dt);
for(int i = 0; i < dt->Rows->Count-1; i++)
{
String^ value_string;
value_string=dt->Rows[i]->ToString();
Console::WriteLine(value_string);
}
cn->Close();
Console::ReadLine();
but it only returns a lot of "System.Data.DataRow".
Can someone help me?
(I need to put it into a matrix form after I extract the binary data, so if anyone could provide help for that part as well, it'd be highly appreciated!)
dt->Rows[i] is indeed a DataRow ^. To extract a specific field from it, use its indexer:
array<char> ^blob=dt->Rows[i][0];
This extracts the first column (since you have only one) and returns an array representation of it.
To answer the question in your code, the way SqlDataAdapter works is like this:
you build a DataTable to hold the data to retrieve. You can fill in its columns, but you're not required to. Neither are you required to give it a name.
you build the adapter object, giving it a query and a connection object
you call the Fill method on the adapter, giving it the previously created DataTable to fill with whatever your query returns.
and you're done with the adapter. At this point you can dispose of it (for example inside a using statement if you're using C#).

MySQL/C++ and Prepared Statements: setInt always 0

I'm using the MySQL Connector/C++ library to insert values into a database table. I'm following the examples at
http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html
almost exactly. However, I can't seem to get prepared statements to work with value placeholders.
sql::mysql::MySQL_Driver* driver = sql::mysql::MySQL_Driver::Instance();
boost::shared_ptr<sql::Connection> conn(driver->connect("localhost", "", ""));
conn->setSchema("TESTDB");
boost::shared_ptr<sql::Statement> stmt(conn->createStatement());
stmt->execute("DROP TABLE IF EXISTS TESTTBL");
stmt->execute("CREATE TABLE TESTTBL (m_id INT)");
boost::shared_ptr<sql::PreparedStatement> pstmt(conn->prepareStatement("INSERT INTO TESTTBL VALUES(?)"));
for (int i = 0; i != 10; ++i) {
pstmt->setInt(1, i);
pstmt->executeUpdate(); // Always inserts 0.
}
Any ideas on why I can't bind to the prepared statement? The other set* functions have the same result (e.g. if I use setString I get a string '0' in the resulting row).
I haven't done SQL in a while, but I think you're forgetting to put the column name there. The syntax should be:
INSERT INTO TESTTBL (column_name) VALUES (?)
If you wanted to insert into multiple columns, you'd use comma delimiters:
INSERT INTO TESTTBL (col1, col2, col3) VALUES (?,?,?)
Recompiling the C++ connector from source fixed this problem.
Probably a compiler setting in their pre-built binary that didn't agree with my project. I will talk to MySQL about this.
Thanks for the assistance.