Teradata create table auto-increment column error - mysql

I am trying to import my CSV file into Teradata using Teradata's Fastload script.
I also tried adding an auto-increment column.
This is my CSV file:
Word,country,sale,week
hi,USA,26.17,11/22/15-11/28/15
bye,USA,16.5,11/22/15-11/28/15
code snippet
String tableName = "my_db.mytable";
String createTable = "CREATE TABLE " + tableName + "," +
"NO FALLBACK," +
"NO BEFORE JOURNAL," +
"NO AFTER JOURNAL," +
"CHECKSUM = DEFAULT" +
"(" +
" id decimal(10,0) NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 NO CYCLE),"+
" word VARCHAR(500) CHARACTER SET UNICODE," +
" country VARCHAR(50)," +
" sale FLOAT," +
" week VARCHAR(30)" +
") " +
"PRIMARY INDEX (id)";
// INSERT statement
String insertTable = "INSERT INTO " + tableName + " VALUES(?,?,?,?,?)";
Error i got:
Row 1 in FastLoad table my_db.mytable_ERR_1 contains the following data:
ErrorCode=2673
ErrorFieldName=F_id
ActualDataParcelLength=55
DataParcel: byte array length 55 (0x37), offset 0 (0x0), dump length 55 (0x37)

This doesn't look like a FastLoad script, is this part of a JDBC-FastLoad?
2673 The source parcel length does not match data that was defined.
Your input data is a comma-delimited text, thus you must define all columns as VARCHAR.
And there are four columns in your input file, but you specify five in the INSERT. As the name implies a GENERATED ALWAYS sequence is automatically created.

Related

MYSQL Truncated incorrect DOUBLE while using double and single quotation marks

I'm created a trigger that inserts a row in a table after a row is deleted in another table.
Here's my trigger
CREATE TRIGGER trigger_delete_log_animal AFTER delete ON animal
FOR EACH ROW
INSERT INTO log_animais (momento, ocorrencia)
VALUES (now(), "The register '" + old.nome_animal + "' was deleted from the animal table");
I want nome_animal to be between single quotation marks.
But I get the following error when I delete a row from the animal table:
Error Code: 1292. Truncated incorrect DOUBLE value: 'The register ''
I've tried changing it to
'The register "' + old.nome_animal + '" was deleted from the animal table'
And also to
"The register \'" + old.nome_animal + "\' was deleted from the animal table"
But it's no good.
What am I doing wrong?
Don't try to build strings with + in your SQL code.
Use CONCAT() instead:
VALUES (now(), CONCAT(
'The register \'',
old.nome_animal,
'\' was deleted from the animal table'));
And, escape the ' characters you want in your strings with \.
'Mrs. O\'Leary\'s cow.'

MySQL multiply a row by a variable

I am trying to multiply a row by a variable (calculated amount):
double servingsMultiplier = 1;
double servingSizeMultiplier = 1;
Calculate the values for "servingsMultiplier" and "servingSizeMultiplier".
String selectQry5 = ("SELECT ci_id, cr_id, ci_ingedient, (ci_amount*servingsMultiplier) AS ci_amount, " +
" (ci_unit*servingSizeMultiplier) AS ci_unit " +
" FROM at_cat_ingredient " +
" WHERE cr_id = ? " +
" ORDER BY ci_ingedient;");
The above works when I use a constant (e.g., 2); however, not when I use a variable. I get the error message:
"SQLException in recipePDF:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
column 'servingsMultiplier' in 'field list'.
An identifier like servingsMultiplier inside the sql statement is not recognized as the value of the variable but as a column name, which of course does not exist.
Use ? placeholders for servingsMultiplier and servingSizeMultiplier in the statement and pass their values just like you pass the parameter in the WHERE clause:
String selectQry5 =
"SELECT ci_id, cr_id, ci_ingedient, " +
"(ci_amount * ?) AS ci_amount, " +
"(ci_unit * ?) AS ci_unit " +
"FROM at_cat_ingredient " +
"WHERE cr_id = ? " +
"ORDER BY ci_ingedient;";
If you want to use mysql variables, then add # before variable name.
SELECT ci_id, cr_id, ci_ingedient, (ci_amount*#servingsMultiplier) AS ci_amount,
(ci_unit*#servingSizeMultiplier) AS ci_unit
FROM at_cat_ingredient
WHERE cr_id = #id
ORDER BY ci_ingedient;

Is there a way to do what is in the third and fourth instance in a different way this doesn't work

The code is about employee salary management, but we are also trying to automate some of the code by not requiring the user to input the proper syntax but rather the just the values such as in a create table statement.
import mysql.connector as mysql
while True:
print("1.C&L")
print("2.Cursor Define")
print("3.Create Table")
print("4.Insert Data into Table")
a=int(input("Enter Choice(0 to exit)"))
if a==0:
break
if a==1:
db=mysql.connect(
host='localhost',
user='root',
passwd='12345',
database='opip'
)
print(db)
if a==2:
curs=db.cursor()
print(curs)
if a==3:
print("CREATE TABLE [table_name] ([column_name] [data_type][data_size])")
z=input("Type the above syntax with correct variables")
curs.execute(z)
if a==4:
x="""INSERT into opip(id,Name,Age,DOB,Position,Salary)VALUES(%s,%s,%s,%s,%s,%s)"""
data=input("Provide Values")
curs.execute(x,data)
db.commit()
You can accomplish this in two ways
Recursively read the input for each of your placeholders [table_name],[column_name],[data_type],[data_size] until an end flag determined by you is read.
table=input("Table Name ")
columnName=input("Column Name ")
columnType=input("Column Data Type ")
columnSize=input("Column Size ")
Read everything in a single input and then split it into respective types to populate your template
txt = input()
values = txt.split()
dbString = "CREATE TABLE " + values[0] + "(" # Start Syntax
i=1
while i < len(values):
dbString += values[i] + " " # Column Name
i += 1
dbString += values[i] + "(" # Column Data Type
i += 1
dbString += values[i] + ")" # Column Size
i += 1
if i < len(values):
dbString += ", " # prepare for next column details
dbString += ")" # End Syntax
curs.execute(dbString) # Execute Dynamic SQL
For Input
mytable col1 char 2 col2 char 3
DbString Output
CREATE TABLE mytable(col1 char(2), col2 char(3))

Data truncation error using mysql jdbc when inserting data from csv file to table

I am inserting data from a .csv file to my MySQl database table in eclipse using "load data local infile". However, I am getting the error message shown below.
Exception in thread "main" com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '1894' for column 'startYear' at row 1
Sample Data:
tconst_titles,titleType,primaryTitle,originalTitle,isAdult,startYear,endYear,runtimeMinutes
tt1,short,Carmencita,Carmencita,0,1894,0000,1
tt2,short,Le clown et ses chiens,chiens,0,1892,0000,5
A similar thread mentioned that it has to do with the date values not being in the correct format. However, I have declared "startYear" as a date when creating the table and it should recognize '1894' as a year shouldn't it ?
Code for creating the table:
String sql1 = "CREATE TABLE Titles" +
"(tconst_titles VARCHAR(255) PRIMARY KEY, " +
" titleType VARCHAR(255), " +
" primaryTitle VARCHAR(255), " +
" originalTitle VARCHAR(255), " +
" isAdult TINYINT(1), " +
" startYear DATE, " +
" endYear DATE, " +
" runtimeMinutes INT)";
Code for inserting data from .csv file:
import java.sql.*;
public class populate {
public static void main(String[] args) throws SQLException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/IMDB","root","user");
Statement stmt = con.createStatement();
String sql =
"load data local infile 'titles.csv' \n" +
" replace \n" +
" into table Titles \n" +
" columns terminated by '\\t' \n" +
" ignore 1 lines";
stmt.execute(sql);
}
}
https://dev.mysql.com/doc/refman/5.7/en/datetime.html says:
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
Your data is only YYYY. This has no month or day, so it's not a date in the format required by MySQL.
If you only want to store a year with no month or day, use the YEAR data type if you have values in the supported range of 1901 - 2155.
If you have other years (like you have 1894), use SMALLINT UNSIGNED.

Optimizing hive queries

I am trying to optimize hive query. I have partitioned and stored my base table as ORC file as shown below.
create table if not exists processed (
plc string,
direction string,
table int,
speed float,
time string
) PARTITIONED BY (time_id bigint) STORED AS ORC;
I am firing the below query on the above table (contains 500.000 records). The final result I get is stored as a json. The whole transaction takes about 35 secs. Is there a way wherein I can reduce this time. Or may be, someone could suggest me using a different framework instead of Hive. This is the query :
String finalQuery = "select plc,direction,AVG(speed) as speed ,COUNT(plc) as count,time_id from processed WHERE plc IN "
+ " "
+ "("
+ plcCSV
+ ")"
+ " " + " " + "AND" + " " + "time_id =" + " " + time_id + " "
+ "group by plc,direction,time_id";
First of all create an index on plc column and then try.