i have database in csv format, and i want to calculate fare from one station to another station but problem is database, i think is not in proper format to build SQL query, so can we try sql query from below database format to fetch fare price from versova to sakinaka as an example.
also is there any other solution table structure which will be friendly for firing sql queries on it.
Load your CSV into a System.Data.DataTable (C#).
Create Table T_Hop
HOP_Id integer not NULL
,HOP_Name nvarchar(200)
Create Table T_Fare
Fare_Id integer not null
,Fare_Departure_HOP_Id integer not null
,Fare_Destination_HOP_Id integer not null
,Fare_Fare numeric(10, 2) not null
take first row
foreach column ==> add hop name with id = ordinal index of column
for(int i=0; i < dt.Columns.Count; ++i)
{
for(int j=0; j < dt.Rows.Count; ++j)
{
double Fare = System.Convert.ToDouble(dt.Rows[j][i]);
==> insert into T_Fare(Fare_Id,Fare_Departure_HOP_Id, Fare_Destination_HOP_Id, Fare_Fare ) Values (j*dt.Columns.Count + i, i, j, Fare)
// and check that I didn't mix up i and j
}
}
Query:
SELECT Fare_Fare
WHERE Fare_Destionation_HopId = x
and Fare_Departure_Hop_Id = y
Related
I want to insert 6406 recods into a table in one swoop without having to prepare and execute individual records. (fastest). In the code below, I want "i" to change for every insert
Ive tried everything
for( int i = 0; i < 6406 ; i++)
{
sprintf( query, "INSERT INTO table1"
"(table_id, curr_id, cur_ref_id) "
"VALUES (%d,%d,%d)",
table_id,
i,
table_id);
//assemble query
DBH->prepare(query);
}
DBH->execute();
Inset prepare failed for table1
My memory of the MySQL C API is a bit fuzzy, but IIRC it should look something like:
MYSQL_STMT* stmt = mysql_stmt_init(MYSQL *mysql);
mysql_stmt_prepare(stmt,
"INSERT INTO table1 (table_id, curr_id, cur_ref_id) VALUES (?,?,?)",
len_of_previous_argument);
MYSQL_BIND params[3];
memset(params, 0, sizeof(params));
params[0].buffer_type = MYSQL_TYPE_LONG;
params[1].buffer_type = MYSQL_TYPE_LONG;
params[2].buffer_type = MYSQL_TYPE_LONG;
params[0].buffer = &table_id;
params[2].buffer = &table_id;
mysql_bind_param(stmt, params);
mysql_autocommit(conn, 0);
for ( int i = 0; i < 6406 ; i++) {
params[1].buffer = &i;
mysql_stmt_execute(stmt);
}
mysql_commit(conn);
mysql_stmt_close(stmt);
You'll obviously want to throw in some error handling, but this should give you the basic idea.
It might be faster to only have one parameter and encode the table_id values in the query string instead, but I'm lazy and you obviously know how to do that already (snprintf should have "(%d,?,%d)", then pass the result to mysql_stmt_prepare, then the params array will only be a single item).
while converting Json to table in Mysql having 50000 line item... It take long time to execute (20 mins).Below is patch of code... Please help me...
Set I_InvoiceItemDetails = JSON_UNQUOTE(JSON_EXTRACT(p_data, '$.InvoiceItemDetails'));
While (J < JSON_Length(I_InvoiceItemDetails)) DO
SET B = JSON_UNQUOTE(JSON_EXTRACT(I_InvoiceItemDetails, CONCAT('$[', J, ']')));
SET I_InvoiceNo = JSON_UNQUOTE(JSON_EXTRACT(B, '$.InvoiceNo'));
Insert Into temp_InvoiceitemDetailsData (_InvoiceNo)
Values (I_InvoiceNo)
Select J + 1 Into J;
End While;
for (int n = 0; n < scoreDocs.Length; ++n)
{
doc = new Document();
ScoreDoc sd = scoreDocs[n];
float score = sd.Score;
int docId = sd.Doc;
doc = searcher.Doc(docId);
string userId = doc.GetField("userID").StringValue;
UserID id = new UserID();
id.user_ID = Convert.ToInt32(userId);
QueryUsers.Add(id);
}
I want to send this List of Id s to Mysql DB , so how can I receive there in my sql Stored procedure ? please writ Stored procedure thanks
Refer to this examples, it will help you :
JDBC CallableStatement – Stored Procedure IN parameter example
Calling Stored Procedures in JDBC Programs
I create stored procedure in mysql which should select latitude, longitude values from table iterate over that result and calculate distance of 2 coordinates.
How can I iterate over the select result and get current and next rows columns value like java ?
double d = 0;
for (int i = 0; i < locations.size-1; i++) {
d += locations.get(i).getLat() + locations.get(i+1).getLat();
}
I think this my help
http://dev.mysql.com/doc/refman/4.1/en/connector-j-usagenotes-statements-callable.html
You will find at Example 17.7 the loop over your procedure.
This is my function for all the data from my database. I only want the last four data out of the database.
function spelerWeergeven()
{
db.transaction(function(tx)
{
tx.executeSql('SELECT * FROM speler', [], function (tx, results)
{
var len = results.rows.length, i;
if(len > 0)
{
$('#spelerTabel').replaceWith('<table id="spelerTabel"><tr><th>Spelers</th></tr></table>');
$('#spelerTabel').hide();
for(var i = 0; i< len; i++)
{
$('#spelerTabel tr:last').after('<tr><td>'+results.rows.item(i).naam+'</td></tr>');
}
$('#spelerTabel').show('slow');
}
}, null);
}
);
}
Does anybody know the answer?
SELECT * FROM `speler` ORDER BY `id` DESC LIMIT 4;
This should work
SELECT *
FROM `speler`
ORDER BY `id` DESC
LIMIT 4;
(The backticks (`) are used to prevent SQL from reading the words between them as a keyword. It's a good thing to keep that in mind, since there are many, many keywords in SQL.)
Edit:
Since you have created a table called speler with the columns id (which is the primary key) and naam using the query CREATE TABLE IF NOT EXISTS speler (id INTEGER PRIMARY KEY, naam), we know your primary key column is id.
PS: It's a good thing to let table names start with capital letter (Speler), by convention.
This will do it server side:
SELECT TOP 4 * FROM
speler
ORDER BY id DESC
Also pretty clean using Linq:
//dc would be a linq datacontext
string cmd = #"SELECT TOP 4 * from speler order by id DESC";
IEnumerable<speler> fourItems = dc.ExecuteQuery<speler>(cmd);