Setting PostgreSQL enums using a JDBC ResultSet - postgresql

I'm trying to copy data from a MySQL database to an equivalent PostgreSQL database, doing a load/insert through Java using JDBC. I get this error whenever I try to copy an enum column:
org.postgresql.util.PSQLException: ERROR: column "mycol" is of type mytable_mycol_enum but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 194
The Java code (simplified and anonymized):
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection mysqlConn = DriverManager.getConnection(
"jdbc:mysql://localhost/mysqldb", "user", "pass");
Class.forName("org.postgresql.Driver").newInstance();
Connection pgConn = DriverManager.getConnection(
"jdbc:postgresql://othercomp/pgdb", "user", "pass");
Statement selStatement = mysqlConn.createStatement();
ResultSet selSet = selStatement.executeQuery("SELECT * FROM mytable");
Statement insStatement = pgConn.createStatement(
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
Resultset insSet = insStatement.executeQuery("SELECT * FROM mytable WHERE 0=1");
insSet.moveToInsertRow();
while(selSet.next())
{
ResultSetMetaData metaData = selSet.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++)
{
String colName = metaData.getColumnName(i);
Object obj = selSet.getObject(colName);
insertSet.updateObject(colName, obj);
}
insertSet.insertRow();
}
This works fine for tables without an enum column, but
In MySQL I have a column mycol of type enum('A','B','C')
In PostgreSQL I have mycol of type mytable_mycol_enum with CREATE TYPE mytable_mycol_enum AS ENUM ('A','B','C')
How do I set the enum value?

You have to cast the value, so that postgresql understands it:
insert into mytable (mycol) values ('A'::mytable_mycol_enum)
You might have to use a named insert statement, not the generic one in your example. Also, try it in psql first to see if it works.
HTH

Related

How to use Store procedure in entity frame work

I have created store procedure in Sql server its working.when i imliment it into my entity framework, its throws exception, I am new to this, kindly suggest
//SQL//
create procedure sp_getUserID
#deviceID int,
#userID int out
as
Begin
Select #userID= userId from UserTable
where deviceID = #deviceID
End
// C#
var UserID =0;
// This line error ERROR: //The specified parameter name '#userID' is not valid.
System.Data.Entity.Core.Objects.ObjectParameter UserOutput = new System.Data.Entity.Core.Objects.ObjectParameter("#userID", typeof(int));
var objStoredProcd = dbContext.sp_getUserID(UserOutput, UserLogin.DeviceUUID);
UserID = Convert.ToInt32(UserOutput.Value);

function does not exist in pg_proc in postgresql

I tried to call my user defined function in pgresql from C# code,
my function creation script is as follows,
CREATE OR REPLACE FUNCTION public."GetUserDailyData"(
cid integer,
hid integer,
currday integer)
RETURNS character varying AS
$BODY$
DECLARE
returndata varchar = '';
BEGIN
SELECT data->20+currday into returndata FROM pops
WHERE hybid = hid and cropid = cid;
return returndata;
END
$BODY$
LANGUAGE plpgsql
COST 100;
My method to call this function is as follows,
public static object ExecuteScalar(string conString, string spName, NpgsqlParameter[] param)
{
using (var conn = new NpgsqlConnection(conString))
{
conn.Open();
using (var tran = conn.BeginTransaction())
using (var command = conn.CreateCommand())
{
command.CommandText = spName;
command.CommandType = CommandType.StoredProcedure;
for (var i = 0; i < param.Length; i++)
{
command.Parameters.Add(new NpgsqlParameter());
command.Parameters[i] = param[i];
}
var result = command.ExecuteScalar();
return result;
}
}
}
I tried everything even checked the existence of this function in pg_proc using
select * from pg_proc where proname = 'GetUserDailyData'
and it reflected the function details row.
But every time it is giving the same error.
Any kind of suggestion would be highly appreciated. Thanks.
Adding objects with case sensitive names in PostgreSQL can lead to these complications; in this case you need to specify the name of the stored procedure between quotes, however it would be advisable to simply not create any objects that rely on case sensitivity, use underscores instead, or when create/refer to objects using CamelCase without the quotes (which creates/refers to the objects in low-caps). In any case, you may also need to specify the whole interface (not just the name) as the CommandText, and specify the data types of the parameters (see this).
...
command.CommandText = "\"" + spName + "\"";
...

Postgres insert strings with null between the characters

I got a table where the data may contain null between the characters. As I have already defined the table as VARCHAR, it throws me an error
org.postgresql.util.PSQLException: ERROR: invalid byte sequence for
encoding "UTF8": 0x00
There should be a way where I can insert a null based string in postgres.
This is the sample insert that has failed while inserting onto postgres
private void postGrestest() throws ClassNotFoundException, SQLException
{
Class.forName("org.postgresql.Driver");
String dropStmt = "DROP TABLE PUBLIC.TEST";
String createStmt = "CREATE TABLE PUBLIC.TEST(COL1 VARCHAR(50), COL2 BOOLEAN)";
String insertStmt = "INSERT INTO PUBLIC.TEST VALUES (?, ?)";
try (Connection connection = DriverManager.getConnection(
"jdbc:postgresql://url:5432/objectserver?stringtype=unspecified",
"username", "password");
Statement stmt = connection.createStatement();
PreparedStatement ps = connection.prepareStatement(insertStmt);)
{
stmt.execute(dropStmt);
stmt.execute(createStmt);
Random r = new Random();
for (int i = 0; i < 100; i++)
{
Object str = "Test" + i;
str = ((String) str).replace('s', '\0');
logger.info("Inserting " + str);
// str = ((String) str).replace("\0", "");
ps.setObject(1, str);
Object obj = String.valueOf(r.nextBoolean());
ps.setObject(2, obj);
ps.executeUpdate();
}
}
}
Are there any considerations before dealing with this type of data? This data is a string based one where the source may contain data containing null between them. This is handled well on a different database instance SQL Server using NVARCHAR.
You can't include a null in a string in PostgreSQL. From the documentation:
The character with the code zero cannot be in a string constant.
Java uses a slightly modified Unicode scheme where U+0000 can be encoded as 0xC0 0x80, a two-byte encoding. You might replace these values in the string rather than a binary null. PostgreSQL will gladly ingest it.

PostgreSQL Parameterized Insert with ADO.NET

I am using NpgSQL with PostgreSQL and ADO.NET. Forgive the simplicity of the question as I just started using PostgreSQL and NpgSQL this week.
Something like this works fine:
[Test]
public void InsertNoParameters()
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
conn.Open();
IDbCommand command = conn.CreateCommand();
string sql = "INSERT INTO Customers (FirstName,LastName) VALUES ('Test','Tube')";
command.CommandText = sql;
command.ExecuteNonQuery();
conn.Close();
}
When I put in parameters I get the error message:
Npgsql.NpgsqlException : ERROR: 42703: column "_firstname" does not exist
[Test]
public void InsertWithParameters()
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
conn.Open();
IDbCommand command = conn.CreateCommand();
string sql = "INSERT INTO Customers (FirstName,LastName) VALUES (_FirstName,_LastName)";
command.CommandText = sql;
var parameter = command.CreateParameter();
parameter.ParameterName = "_FirstName";
parameter.Value = "Test";
command.Parameters.Add(parameter);
parameter = command.CreateParameter();
parameter.ParameterName = "_LastName";
parameter.Value = "Tube";
command.Parameters.Add(parameter);
command.ExecuteNonQuery();
conn.Close();
}
The responses in the comments are correct:
Npgsql doesn't support _ as a parameter placeholder notation. You should be using # or : (so #FirstName or :FirstName, not _FirstName).
PostgreSQL will automatically lower-case your table and column names unless they are double-quoted. Either use lower-case names for everything (simpler) or quote identifiers in your SQL queries.
So your code should look more or less like this:
IDbCommand command = conn.CreateCommand();
string sql = "INSERT INTO Customers (first_name, last_name) VALUES (#FirstName,#LastName)";
command.CommandText = sql;
var parameter = command.CreateParameter();
parameter.ParameterName = "FirstName";
parameter.Value = "Test";
command.Parameters.Add(parameter);

Using DB2, "statement.executeQuery()" throws SqlEception only in my system

//Class UserProfileDBUtil.java
Connection conn = null;
PreparedStatement statment = null;
ResultSet rs = null;
try{
String sqlString = "select count(*) from "+AGENCY_SCHEMA_NAME+".AGENCY WHERE AGENCYCODE= ? and ENABLEDFLAG = ?";
conn = JDBCUtils.getConnection(JDBCUtils.WP_ODS);
statment = conn.prepareStatement(sqlString);
statment.setString(1, agencyCode.toUpperCase());
statment.setString(2, "Y");
rs =statment.executeQuery(); // SQL Error Line 42
while(rs.next())
{
if(rs.getInt(1) > 0 )
{
return true;
}
}
log.error("Agency for agency code "+agencyCode+" is not available or not active");
}
LOG :
com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -401, SQLSTATE: 42818, SQLERRMC: =
at com.ibm.db2.jcc.b.sf.e(sf.java:1680)
at com.ibm.db2.jcc.b.sf.a(sf.java:1239)
at com.ibm.db2.jcc.c.jb.h(jb.java:139)
at com.ibm.db2.jcc.c.jb.a(jb.java:43)
at com.ibm.db2.jcc.c.w.a(w.java:30)
at com.ibm.db2.jcc.c.cc.g(cc.java:161)
at com.ibm.db2.jcc.b.sf.n(sf.java:1219)
at com.ibm.db2.jcc.b.tf.gb(tf.java:1818)
at com.ibm.db2.jcc.b.tf.d(tf.java:2294)
at com.ibm.db2.jcc.b.tf.X(tf.java:508)
at com.ibm.db2.jcc.b.tf.executeQuery(tf.java:491)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeQuery(WSJdbcPreparedStatement.java:559)
at UserProfileDBUtil.isAgencyActive(UserProfileDBUtil.java:42)
The error you're getting is a -401, which means:
The data types of the operands for the operation operator are not
compatible or comparable.
I would check and make sure that the parameters you're passing in are using the right data types. If you catch the exception, you should be able to use the exceptions Message property to see what the operator is. See here for an example.