I have inserted a lot of information into a SQlite database from my program. Now, my small task is to look up the database and find some statistics from it.
Ex: I have a database with three fields - Timestamp, Message and Key Now, I want to check the database in the Message column and count the number of times a certain keyword(say "ERROR") occurs in the database.
What is the simplest way to do this? SELECT Message from Database and how do i add the filter???
I use Regex to filter out some stuff before Insert. Should i do something similar here?
Thanks
Edit:
I used the solution mentioned below SELECT count(*) but am still unaware of how to get the value out to use it in another function. I have shown the code i used below. I am new to SQL and it possibly is wrong. Please help!
public void selectFromDatabase()
{
String cntnStr_query = "data source=C:\\TMU_Files\\test31.s3db";
SQLiteConnection connection_query = new SQLiteConnection(cntnStr_query);
connection_query.Open();
SQLiteCommand cmd_query = connection_query.CreateCommand();
cmd_query.CommandText = "SELECT count(*) from LogDatabase where LogMessage like '%ERR%'";
SQLiteDataAdapter da_query = new SQLiteDataAdapter(cmd_query);
DataSet ds_query = new DataSet();
//MessageBox.Show(cmd_query.ExecuteNonQuery().ToString());
//MessageBox.Show(count.ToString());
try
{
da_query.Fill(ds_query);
DataTable dt_query = ds_query.Tables[0];
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
cmd_query.Dispose();
connection_query.Close();
}
//cm开发者_StackOverflowd_query.Dispose();
//connection_query.Close();
}
}
}
select count(*) from database where message like '%error%'
As to how it should look like in code, try something like this:
using(var sda=new SQLiteDataAdapter(
"SELECT count(*) from LogDatabase where LogMessage like '%ERR%'",
"data source=C:\\TMU_Files\\test31.s3db")
{
var dt=new DataTable();
sda.Fill(dt);
return dt;
}
SQLite uses a subset of the SQL-92 language, so most of the basics are the same as in other SQL environments. SELECT
, FROM
, WHERE
, ORDER BY
clauses all work mostly like you'd expect.
Documentation on the syntax supported can be found here: SQL As Understood By SQLite
Number of time error appears is given by something like this...
select sum((Length(message) - length(replace(message,'error',''))) / length('error')) from
from database where
message like '%error%'
精彩评论