i need to get column name from the json array and ccompare with the column name with the sqlite table and have to alter the table when the column name dowsnot exists.
i use the code to fetch the column and column name from sqlite by this code
String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null );
JSONArray resultSet = new JSONArray();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();
for( int i=0 ; i< totalColumn ; i++){
cursor.getColumnName(i);
here i use "cursor.getColumnName() " to get the column name from the sqlitetable.
like this is this possible to fetch the column name from the JSONArray
example
cursor.getColumnName(resultSet.getJSONArray(i);
is this possible, else give me solution to get the column name of JSONArray value.
i have to compare the column names of JSONArray and the sqlite table.
Related
I have the follow:
def getIds(name: String): java.sql.Array = {
val ids: Array[Integer] = Array()
val ps: PreparedStatement = connection.prepareStatement("SELECT id FROM table WHERE name = ?")
ps.setString(1, name)
val resultSet = ps.executeQuery()
while(resultSet.next()) {
val currentId = resultSet.getInt(1)
ids :+ currentId
}
return connection.createArrayOf("INTEGER", ids.toArray)
}
My intention is to use this method output to put into another PreparedStatement using .setArray(1, <array>)
But I'm getting the follow error: java.sql.SQLFeatureNotSupportedException
I'm using MySQL. Already tried INTEGER, INT, BIGINT. No success with none of then.
Researching more found this:
It seems that MySQL doesn't have array variables. May U can try temporary tables instead of array variables
So my solution was to create a temp table with just ids:
val idsStatement = connection.prepareStatement(
"CREATE TEMPORARY TABLE to_delete_ids SELECT id FROM table WHERE name = ?")
idsStatement.setString(1, name)
idsStatement.executeUpdate()
Than do inner join with other statments/queries to achieve same result:
val statementDeleteUsingIds = connection.prepareStatement(
"DELETE to_delete_rows FROM table2 to_delete_rows INNER JOIN to_delete_ids tdi ON tdi.id = to_delete_rows.other_tables_id")
statementDeleteUsingIds.executeUpdate()
Given that I have a Google sheet similar to this:
ID;username,email,date_created;
hdJNGDyd;user;me#example.com;12/03/2020 4:20:22
...
Is there a way to determine the formats of the Sheet columns and list them?
eg.
ID - string(8)
username - varchar(34)
email - varchar(255)
date_created - datetime
or map the sheet structure to a SQL query similar to:
CREATE table MyTable
`ID` varchar(8) NOT NULL
...
The length could be for example maximum length of the values in the particular column.
I would create a tab with a table listing the data dictionary
However, sheets has a TYPE() function you can use. If you also wanted to get the max length that would require some additional calculations.
So as you can see there is no date type and only a number type. Much different than what SQL offers.
In some of my projects I use google app script to get the schema from the MySQL database and then act accordingly
function getColTypes(conn, table) {
var stmt = conn.createStatement();
var query = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='" + DATABASE + "' AND TABLE_NAME='" + table + "'";
var rs = stmt.executeQuery(query);
var colTypes = [];
while (rs.next()) {
colTypes.push(rs.getString(1));
}
rs.close();
stmt.close();
return colTypes;
}
And this one for the names
function getColNames(conn, table) {
var stmt = conn.createStatement();
var query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='" + DATABASE + "' AND TABLE_NAME='" + table + "'";
var rs = stmt.executeQuery(query);
var colNames = [];
while (rs.next()) {
colNames.push(rs.getString(1));
}
rs.close();
stmt.close();
return colNames;
}
May anybody tell me how to replace this code using Linq ?
using using Microsoft.Practices.EnterpriseLibrary.Data;
Public IDataReader GetRowByRowData()
{
Database Db = DatabaseFactory.CreateDatabase();
string sqlString = "SELECT * FROM TableTest";
DbCommand DbCmd = PpwDb.GetSqlStringCommand(sqlString);
Db .ExecuteReader(DbCmd);
}
Please help to get row by row data from table TableTest using Linq
you can do that like this:
var myQyery=from a in dataContext.Mytable
select a;
foreach(var item in myQuery)
{
//what you like
}
var records = (from p in context.TableTest
select p).ToList();
foreach(var record in records) {
// loop through each record here
}
ToList method will query the database and get the result set.
I load the primary key from my table into a list. Depending on the size of the data set and the primary key, loading into a list does not take too long. After loading the keys, use FirstOrDefault() with a where clause like so:
var keys = Db.TableTest.Select(x => x.primaryKey).ToList();
foreach (var k in keys)
{
var record = (from i in Db.TableTest
where i.primaryKey == k
select new
{
//Select only the columns you need to conserve memory
col1 = i.col1,
col2 = i.col2
}).FirstOrDefault();
//Process the record
}
I have an error updating my database because of variables. This is my code:
UPDATE `payment` SET `paid`=1 AND `amoun`=$amountpaid WHERE `paid`=0 AND `userid`=$uid
$amountpaid is the amount of the bill that the user paid and $uid is user id. It seems like using $ in front of variable names is forbidden. How can I use variables in SQL?
Where are your variables coming from? You probably want something like this if you're using JDBC:
int setPaid = 1;
int amountPaid = x; // put $amountpaid here
int wherePaid = 0;
int userId = y; // put $uid here
String updateQuery = "UPDATE payment SET paid = ?, amoun = ?"
+ " WHERE paid = ? AND userid = ?";
PreparedStatement ps = con.prepareStatement(updateQuery);
ps.setInt(1, setPaid);
ps.setInt(2, amountPaid);
ps.setInt(3, wherePaid);
ps.setInt(4, userId);
ps.executeUpdate();
I got the solution by using String.
I converted the ArrayList to a String and then sent the data as string. The data got updated but I don't know what will happen next if I want to view the data in the client tier...
I have a table of Contacts, and a table of Groups which has a many-to-many relationship managed by a simple contacts_groups table:
contacts_groupsID Identity INT
ContactID INT
GroupID INT
I have a delimted String of contact IDs e.g. "1|23|987|2346|33|9821|" which I need to insert into the contacts_groups table (along with the groupID). I am using LinQ to SQL and C#, but want to know the most efficient way of looping through the delimited string (probably .Split()) checking for duplicates and inserting if not exist.
List<int> requested = contactIds.Split('|')
.Select(s => int.Parse(s))
.Distinct()
.ToList();
List<int> existing = (
from x in db.GroupsContacts
where x.GroupId == groupId
select x.ContactId
).ToList();
List<int> toBeAdded = requested.Except(existing).ToList();
foreach(int id in toBeAdded)
{
GroupsContacts record = new GroupsContacts();
record.GroupID = groupID;
record.ContactID = id;
db.InsertOnSubmit(record);
}
db.SubmitChanges();