Problem with the integer value in an SQL syntax: #1366 error - sql

I was making a php website with some forms that let you make a form which let's you make an account, but for some reason when I was checking the SQL I was using inside phpmyadmin...
The SQL code it gave me an error that of
#1366 Incorrect integer value
Here is the SQL code I was checking:
INSERT INTO Users VALUES ('','$username','$password','0','empty')
The weird thing about this is the fact that the integer ID, which is in auto-increment, as I've learned it should be blank in the syntax like I've done (the ID column is the first one in my table)

When you are inserting rows into a table, the best practice is to always list the columns:
INSERT INTO Users (col1, col2, col3, col4)
VALUES ('', '$username', '$password', '0', 'empty');
I don't know what your column names are, so you have to fill them in.
If you are writing application code, another best practice is to use proper parameters rather than munging a query string with values.

Related

Anylogic: Write parameter value into Database table

I'm trying to write the value of a parameter in Anylogic into a specific cell of a Database table. The parameter is declared in my main and gets its value through a specific calculation in function. Now I want to store the calculated value in the Database table.
I tried using the
INSERT INTO query (executeStatement("INSERT INTO eu (country, capital) VALUES ('Croatia', 'Zagreb')"); --> example from help)
…but I'm not able to use the specific parameter in the query/as a VALUE. I can only write direct input (like 'Croatia'), but not the parameter. At the end I want the table to get the current value from the parameter and insert the value in the table.
I found an Insert connectivity tool in the help function, but unfortunately it's only available in the professional edition.
Does anyone have an idea how to handle this?
Thank you and have a great weekend!
I don't know if I fully understood what you need but if what you want to do is simply insert values into a table, then what you've done inside your "executeStatement" is actually enough:
INSERT INTO eu (country, capital) VALUES ('Croatia', 'Zagreb')
If what you mean by "specific cell" is actually replacing the content of an existing field in a given row, you want to use UPDATE instead:
UPDATE eu SET country='New country', capital='New capital' WHERE <some criteria matching targeted row>
WARNING: don't forget the WHERE statement or every row of your table will be clobbered with these same new values.
And if what you want to do is inserting one or more new rows filled with something that already exists in the database, then you can construct your dataset directly from a SELECT query:
INSERT INTO eu (country, capital)
SELECT country_field, capital_field
FROM some_table
WHERE some_criteria
The SELECT query following the UPDATE statement can be absolutely anything. It may also refer to the same table if needed. The only requirement is to form rows that have the same structure and same type (at least compatible) than the targeted fields.
You need to adjust your String that defines the SQL statement.
Instead of
"INSERT INTO eu (country, capital) VALUES ('Croatia', 'Zagreb')"
you write
"INSERT INTO eu (country, capital) VALUES ('"+myParam1+", '"+myParam2+"')"
Assuming you have 2 params of type String that hold string text.
Your dbase column type must match the parameter (or variable) type you want to write
Be VERY careful with the ' and " signs as above

How to know which column has changed on UPDATE?

In a statement like this:
update tab1 set (col1,col2)=(val1,val2) returning "?"
I send whole row for update on new values, RETURNING * gives back the whole row, but is there a way to check which exactly column has changed when others remained the same?
I understand that UPDATE rewrites the values, but maybe there is some built-in function for such comparison?
Basically, you need the pre-UPDATE values of updated rows to compare. That's kind of hard as RETURNING only returns post-UPDATE state. But can be worked around. See:
Return pre-UPDATE column values using SQL only
So this does the basic trick:
WITH input(col1, col2) AS (
SELECT 1, text 'post_up' -- "whole row"
)
, pre_upd AS (
UPDATE tab1 x
SET (col1, col2) = (i.col1, i.col2)
FROM input i
JOIN tab1 y USING (col1)
WHERE x.col1 = y.col1
RETURNING y.*
)
TABLE pre_upd
UNION ALL
TABLE input;
db<>fiddle here
This is assuming that col1 in your example is the PRIMARY KEY. We need some way to identify rows unambiguously.
Note that this is not safe against race conditions between concurrent writes. You need to do more to be safe. See related answer above.
The explicit cast to text I added in the CTE above is redundant as text is the default type for string literals anyway. (Like integer is the default for simple numeric literals.) For other data types, explicit casting may be necessary. See:
Casting NULL type when updating multiple rows
Also be aware that all updates write a new row version, even if nothing changes at all. Typically, you'd want to suppress such costly empty updates with appropriate WHERE clauses. See:
How do I (or can I) SELECT DISTINCT on multiple columns?
While "passing whole rows", you'll have to check on all columns that might change, to achieve that.

Column changing numeric values to scientific notation by default

In sql server, I am trying to select insert data from one table into another. The code reads as:
Insert into TABLE2 (
Id, document_id
) select id, document_id from TABLE1
These two tables are basically identical. The document id field is in nvarchar50 since we will occasionally get values with a letter in them.
How can i get these to insert as numeric values, instead of scientific notation?
Thank you!
I assume the columns in table1 are of some varchar variant and hold the numbers in scientific notation. You can try to convert them to real (and if necessary to some other numeric or varchar variant form there).
INSERT INTO table2
(id,
document_id)
SELECT convert(real, id),
convert(real, document_id)
FROM table1;
This seems to be an Excel issue, where Excel treats the document id as a number instead as a text. The number formatting of Excel can potentially destroy information, as the same number can be represented in different ways. E.g. what was the original format of 500.2? Was it 0500.200, 000000000500.20 or something else? Also, Excel might even drop decimals, e.g. "5023423423423450" is displayed as "5.02342E+15". There is no way to restore this information in SQL.
You must handle this in Excel by either
entering the document id with a leading apostrophe (') to tell Excel not to interpret it in some way
or by
formatting the document fields as Text before entering the document id.

SQL Server: Inserting from various sources

The simple version
What is the correct syntax of this?
INSERT INTO foo(IP, referer)
VALUES(SELECT bin FROM dbo.bar("foobar"),"www.foobar.com/test/")
I am getting syntax errors near 'SELECT' and ')'
The long version: I want to insert using a Function and a string (this is simplified, in reality there will be a few other values including datetime, ints, etc to insert along with the function result).
I have a function, itvfBinaryIPv4, which was set up to convert IPs to a binary(4) datatype to allow for easy indexing, I used this as a reference: Datatype for storing ip address in SQL Server.
So this is what I am trying to accomplish:
INSERT INTO foo (IP, referer)
VALUES(SELECT bin FROM dbo.itvfBinaryIPv4("192.65.68.201"), "www.foobar.com/test/")
However, I get syntax errors near 'SELECT' and ')'. What is the correct syntax to insert with function results and direct data?
It should be like this -
INSERT INTO foo (IP, referer)
SELECT bin, 'www.foobar.com/test/'
FROM dbo.itvfBinaryIPv4('192.65.68.201')
here assume dbo.itvfBinaryIPv4("192.65.68.201") is table valued function.
The INSERT command comes in two flavors:
(1) either you have all your values available, as literals or SQL Server variables - in that case, you can use the INSERT .. VALUES() approach:
INSERT INTO dbo.YourTable(Col1, Col2, ...., ColN)
VALUES(Value1, Value2, #Variable3, #Variable4, ...., ValueN)
Note: I would recommend to always explicitly specify the list of column to insert data into - that way, you won't have any nasty surprises if suddenly your table has an extra column, or if your tables has an IDENTITY or computed column. Yes - it's a tiny bit more work - once - but then you have your INSERT statement as solid as it can be and you won't have to constantly fiddle around with it if your table changes.
(2) if you don't have all your values as literals and/or variables, but instead you want to rely on another table, multiple tables, or views, to provide the values, then you can use the INSERT ... SELECT ... approach:
INSERT INTO dbo.YourTable(Col1, Col2, ...., ColN)
SELECT
SourceColumn1, SourceColumn2, #Variable3, #Variable4, ...., SourceColumnN
FROM
dbo.YourProvidingTableOrView
Here, you must define exactly as many items in the SELECT as your INSERT expects - and those can be columns from the table(s) (or view(s)), or those can be literals or variables. Again: explicitly provide the list of columns to insert into - see above.
You can use one or the other - but you cannot mix the two - you cannot use VALUES(...) and then have a SELECT query in the middle of your list of values - pick one of the two - stick with it.
Haven't checked it, but the correct syntax would be:
INSERT INTO foo (IP, referer)
SELECT bin, "www.foobar.com/test/" FROM dbo.itvfBinaryIPv4("192.65.68.201")

sqlite data from one db to another

I have 2 sqlite databases, and I'm trying to insert data from one database to another. For example, "db-1.sqlite" has a table '1table' with 2 columns ('name', 'state'). Also, "db-2.sqlite" has a table '2table' with 2 columns ('name', 'url'). Both tables contain a list of 'name' values that are mostly common with each other but randomized, so the id of each row does not match.
I want to insert the values for the 'url' column into the db-1's table, but I want to make sure each url value goes to its corresponding 'name' value.
So far, I have done this:
> sqlite3 db-1.sqlite
sqlite> alter table 1table add column url;
sqlite> attach database 'db-2.sqlite' as db2;
Now, the part I'm not sure about:
sqlite> insert into 1table(url) select db2.2table.url from db2.2table where 1table.name==db2.2table.name
If you look at what I wrote above, you can tell what I'm trying to accomplish, but it is incorrect. If I can get any help on the matter, I'd be very grateful!!
The equality comparison operator in SQL is =, not ==.
Also, I suspect that you should be updating 1table, rather then inserting in it.
Finally, your table names start with digits, so you need to escape them.
This SQL should work better:
update `1table`
set url = (select db2.`2table`.url
from db2.`2table`
where `1table`.name = db2.`2table`.name);