This question already has answers here:
How to count number of columns in a table in SQLITE?
(5 answers)
Closed 8 years ago.
How can I get column count in table using Sqlite. I tried with SELECT count(*) FROM information_schema.COLUMNSC WHERE table_name = 'test' AND TABLE_SCHEMA = "test" it works fine for sql but doesnt works for Sqlite. Is there any other way for getting column count. Thank you..!
Try pragma table_info(table_name); which will give all the columns information-name,type etc. Refer http://www.sqlite.org/pragma.html
I tried without using pragma and it worked to get column count from table in Sqlite following is the code
ResultSet rs = stmt.executeQuery("SELECT * FROM TABLE");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
System.out.println(numberOfColumns);
Related
Apologies for the simple question I am new to using PostgreSQL and Psycopg2. I have two columns in a table I am trying to populate using values from two other tables based on a where conditional. Example code below:
cur = con.cursor()
db_insert = """INSERT INTO "Database"."TABLE_ONE"
("LT_ID_ONE", "LT_ID_TWO")
VALUES(
(SELECT "LT_ID_ONE" FROM "Database"."TABLE_TWO" WHERE "LT_NUM_ONE" =%s),
(SELECT "LT_ID_TWO" FROM "Database"."TABLE_THREE" WHERE "LT_NUM_TWO" =%s)
);"""
insert_values = (df1.iloc[0, 0], df1.iloc[0, 1])
cur.execute(db_insert, insert_values)
When running this command I receive the following error:
psycopg2.errors.NotNullViolation: null value in column "LOT_ID_ONE" violates not-null constraint
DETAIL: Failing row contains (null, null).
Any help would be appreciated.
Looks as though my error was in the order of the elements for the insert_values variable. Once I swapped them it worked. So the SQL code I have is correct.
This question already has an answer here:
Create Pivot view in SQL from a SQL table
(1 answer)
Closed 2 years ago.
I have a table temp and Im trying to query as below :
SELECT
LISTAGG( 'MAX(CASE WHEN CATEGORY = '''||CATEGORY||''' THEN "'||"LEVEL"||'" END) AS "'||
"LEVEL"||'_'||CATEGORY||'"' , ',' ) WITHIN GROUP ( ORDER BY CATEGORY, "LEVEL" DESC
) AS col2
FROM
(
SELECT DISTINCT
"LEVEL",
CATEGORY
FROM
TEMP );
`
I get error as [Code: 1489, SQL State: 72000] ORA-01489: result of string concatenation is too long
Im unable to get rid of this error.
I'm using SQL Commander of DBVisualizer .
I also tried to declare variable before but it does not seem to work:
#ECHO ${col2 ||32767||varchar2}$
I tried to ALTER SYSTEM SET MAX_STRING_SIZE = EXTENDED; which is also giving error : [Code: 2065, SQL State: 42000] ORA-02065: illegal option for ALTER SYSTEM.
Is there anything wrong in the code front if not what could be the workaround for this
If LISTAGG won't work (as, obviously, resulting string is longer than 4000 characters), switch to not-that-elegant XMLAGG which doesn't have that restriction. Result should be the same (compare these two):
SQL> select listagg(dname, ',') within group (order by dname) result
2 from dept;
RESULT
--------------------------------------------------------------------------------
ACCOUNTING,OPERATIONS,RESEARCH,SALES
SQL> select rtrim(xmlagg(xmlelement(e, dname ||',') order by dname).extract
2 ('//text()'), ',') result
3 from dept;
RESULT
--------------------------------------------------------------------------------
ACCOUNTING,OPERATIONS,RESEARCH,SALES
SQL>
I am trying to run a query / select statement and save it in a variable. I know how to get something specific from a specific column but not from counting rows.
This is working as I getting MYID specifically.
ResultSet MYIDrs = stmtCFG.executeQuery( "SELECT rowid, MYID from MYINDEX order by rowid desc limit 1;" );
MYID = MYIDrs.getString("MYID");
Now I am trying to count the rows that works in SQLite client but not in the jdbc as I can't figure out what to request.
this is what I have but is not resulting in what I am expecting.
ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString(?????);
problem or question is: What do I put in the ????? as I already tried everything.
I am expecting to see a number.
I am really sorry I found what I was looking for by assigning a name TOTAL
This is my code and it works...
ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) AS TOTAL from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString("TOTAL");
You use wrong data type. COUNT(*) returns Integer type, Not String.
You can do like this without assigning a label for COUNT(*)
int FILE_COUNT = FILE_COUNTrs.getInt(1); // 1: is the column index of COUNT(*)
This question already has an answer here:
Birt Report Multiple Parameters for the same field
(1 answer)
Closed 6 years ago.
I want to pass one param in birt report and use it in multiple palces , for example :
SELECT * FROM tab1 WHERE startDAte = trunc(sysdate)
UNION
SELECT *FROM tab1 WHERE startDate= trunc(sysdate-1)
So i want to make sysdate like a variable :
SELECT * FROM tab1 WHERE startDAte = trunc(?)
UNION
SELECT *FROM tab1 WHERE startDate= trunc(?-1)
How can i do that ? Thanks
This has already been answered in Reusing an anonymous parameter in a prepared statement
If your database is Oracle, you can also use the following syntax:
WITH params AS
( select ? as p_date,
? as p_whatever,
...
from dual
)
SELECT tab1.* FROM tab1, params WHERE tab1.startDate = trunc (params.p_date)
UNION
SELECT tab1.* FROM tab1, params WHERE startDate= trunc (parms.p_date - 1)
Note that this does not have a negative performance impact, because the DB is clever enough to detect that the params inline-view contains exactly one row.
This question already has answers here:
Add new column to wordpress database
(7 answers)
Closed 7 years ago.
I make a plug-in in word press and create some tables, during upgrading the plug-in i need to add columns to existing table, i want to add columns to table if they are not exist in the table.How can i do so ?
you can simply use this query (change the table name and column name)
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'wp_customer_say' AND column_name = 'say_state'" );
if(empty($row)){
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
}