I know if I want to do this query by cqlengine:
select * from test
I should make a model to test table then do this:
testModel.objects.all()
this code will return all columns in test table but what if I want to do this:
select field1 from test
Is there any way to select optional fields from a table by cqlengine or I should have all column in query?
Thanks for help
You can use the method values_list() method on a queryset in cqlengine to just retrieve the specified values. Simply include the names of the columns to retrieve as string arguments.
So you could do something like:
field1_list = MyModel.objects.all().values_list('field1')
Related
Below is my example code:
db = QSqlDatabase.addDatabase('QSQLITE')
db.setDatabaseName('book.db')
db.open()
model = QSqlQueryModel()
model.setQuery("SELECT * FROM card")
self.tableView.setModel(model)
I am using QSqlQueryModel, Qtablevie, Sqlite3, and able to view all rows in my table. But i want to view only last two rows of my table which are newly inserted rows in to the table. The table has no "id" field and it has numaric and text fields. How is it possible?
Below is the table image:
If you want to get the last 2 elements ordered by any field that indicates the insertion order, in your case "rowid", then you have to use a filter in the SQL command like this:
model.setQuery("SELECT * FROM card ORDER BY rowid DESC LIMIT 2")
Another possible option is to filter the table using QSortFilterProxyModel but it is more inefficient.
I'm working on a website in node js and use SQLite as a database for the first time.
I want to be able to use real for some form data and I noticed that every real in my database are converted to integer once the query is made.
To vizualize the database i am using DB Browser and i checked if the columns are defined as REAL which they are.
If i try to query a data set as 0.1 in my DB I get this :
sqlite> select step_variable
from variables
where id=38;
0.0
After trying as suggested the command TYPEOF(step_variable) it returned :
0.0|real
In the SQLite CREATE TABLE command, one defines a data type affinity, not a data type. SQLite supports the following five column affinities: TEXT, NUMERIC, INTEGER, REAL, NONE.
Thus the data type you specify when creating a table does not enforce a certain data type. You can supply any data type you want or even omit the data type.
CREATE TABLE table1(
column1 ABC,
column2 Others,
column3 WHATEVER);
CREATE TABLE table2(column1, column2, column3);
Populate tables:
INSERT INTO table1 VALUES( 1, 'my text', 123.45);
INSERT INTO table2 VALUES( 1, 'my text', 123.45);
Now let us check what SQLite made out of it:
SELECT column1, TYPEOF(column1) from table1
SELECT column2, TYPEOF(column1) from table1
SELECT column3, TYPEOF(column1) from table1
With:
column TYPEOF(column)
------------------------
1 INTEGER
my text TEXT
123.45 REAL
When you go through a query result e.g. by using sqlite2_step you can use the sqlite3_column_type statement to confirm the column type - unless you know the result anyway and simply cast the result to the data type expected.
Martin
I found the solution it was simply that i didn't save my file after modifying it.
I've defined column like this:
def lastChecked = column[Timestamp]("LAST_CHECKED", O.Default(new Timestamp(System.currentTimeMillis())))
And when I insert data in the table I'm omitting this column. But Slick inserts this column as null value. How this can be fixed?
You need provide default value to field on DB level. For HSQLDB define column in this way:
last_checked TIMESTAMP DEFAULT CURRENT_TIMESTAMP
In slick will be enough to define field with timestamp type:
val lastChecked: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("last_checked")
According to slick documantation O.Default used only for DDL statements.
If user is entering 678.98 then it should display only 678 in one of the field of a window.
I have declare that field as numeric in database and number type reference in tables and columns in openbravo.
Use Math.floor()
import java.lang.Math;
...
value = 678.98;
roundeddown = Math.floor(value); // roundeddown == 678
See also this page about another rounding example.
Working directly with PostgreSql, use the floor() function
To insert data
INSERT INTO table ... VALUES( floor(678.98) ) ...
To select data
SELECT floor( field ) FROM table WHERE ....
Set Integer type reference in tables and columns in openbravo.
I am a beginner in cognos 10. I want to know how to get the value of a prompt from a Query1 and use it on Query2. My requirement is I want a prompt to ask the year for which I want the data in Query1 and in Query2 I want the data relevant to the previous year to that entered in the prompt. How to do that?
You can use the same parameter (tied to the prompt) in filters in both queries. If your parameter is Parameter1 and contains a four-digit year, and your data item in your filter is [Year] then your Query1 filter might look like this:
[Year] = ?Parameter1?
Your Query2 filter would be:
[Year] = ?Parameter1? - 1
Depending on your data source you may have to cast the string parameter to an integer before doing the subtraction though most SQL implementations will implicitly convert the string parameter to an integer for you.