How to Add table in SQliteDatabase - android-sqlite

I want to add a table in database and want to add a column and save information and retrieve it on another button clicked, if table already exists it would just insert information following is the code that is used by me but giving error.
Button insert_info=(Button)findViewById(R.id.button1);
insert_info.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db;
try
{
final EditText txt_field1=(EditText)findViewById(R.id.editText1);
String Per_Name=txt_field1.getText().toString();
db=openOrCreateDatabase("sez.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
String Query_createTable="CREATE TABLE sezInformation(Name VARCHAR(100))";
String Query_insert="insert into sezInformation(Name) values("+Per_Name+")";
db.execSQL(Query_insert);
}
catch(Exception e)
{
Toast.makeText(WelomePerson.this, "ERROR"+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
Whenever I enter the value means khan or any other in layout Edittext field and click on button it shows
No such column :khan while insert into sezInformation (Name) values(khan)
Please also tell me how to retrieve the same saved data in a textview.

Use CREATE TABLE IF NOT EXISTS instead of CREATE TABLE.
Also be sure to use 2 distinct execSQL() commands (one to create the table and one to insert), or it won't work, since you can't execute multiple commands at once.

In SQL, string literals should be in 'single quotes'. Otherwise they are taken as identifiers such as column names.
Even better, use parameters:
db.execSQL("insert into sezInformation(Name) values(?)",
new Object[] { Per_Name });

Related

How to get the column name in nattable?

I had create an example like Editor Example, while the difference is that my demo can hide column. While when I hidden some column, I can not the collect selected column index, I changed my mind, I want to get the selected column's header name. How to get it?
Following is the handler to handle the selected column
But I don't know how to get the column name
public void handleLayerEvent(ILayerEvent event) {
if (event instanceof CellSelectionEvent) {
CellSelectionEvent cellEvent = (CellSelectionEvent) event;
int columnIndex = natTable.getColumnIndexByPosition(cellEvent.getColumnPosition());
SelectionLayer selectionLayer = cellEvent.getSelectionLayer();
........
}
You need the reference to the ColumnHeaderLayer and get the data value. E.g. ColumnHeaderLayer#getDataValueByPosition(int, int)

How do I get changes to save when selecting with a non-EF DTO type?

I'm using the following code to populate a DevExpress XtraGrid with data from a DB first model. Calling SaveChanges has no effect, and I assume this is because there are no cached objects that match the objects I select for the grid.
Is there any way I can tell EF to use the PK, Id, to apply new values to cached objects during saved changes? Calling SaveChanges works if I use the whole Employee object for the grid.
private void EmployeeFormLoad(object sender, EventArgs e)
{
empsGridView.OptionsBehavior.Editable = true;
var emps = context.Employees.Select(emp => new EmployeeDescriptor
{
Id = emp.Id,
FirstName = emp.FullNames,
LastName = emp.Surname
});
employeeDescriptorBindingSource.DataSource = emps.ToList();
}
private void button1_Click(object sender, EventArgs e)
{
employeeDescriptorBindingSource.EndEdit();
context.SaveChanges();
}
Anonymous types are Immutable in C#. In VB, you can make them mutable using the "Key" keyword, although I'm not sure if EF honors that for updates. If you want your model to be editable, change the conceptual model to only include the fields you need and ensure that the unused table columns are all nullable.

GWT RequestFactory + CellTable

Does anyone know for an example of GWT's CellTable using RequestFactory and that table is being edited? I would like to list objects in a table (each row is one object and each column is one property), be able to easily add new objects and edit. I know for Google's DynaTableRf example, but that one doesn't edit.
I searched Google and stackoverflow but wasn't able to find one. I got a bit confused with RF's context and than people also mentioned some "driver".
To demonstrate where I currently arrived, I attach code for one column:
// Create name column.
Column<PersonProxy, String> nameColumn = new Column<PersonProxy, String>(
new EditTextCell()) {
#Override
public String getValue(PersonProxy person) {
String ret = person.getName();
return ret != null ? ret : "";
}
};
nameColumn.setFieldUpdater(new FieldUpdater<PersonProxy, String>() {
#Override
public void update(int index, PersonProxy object, String value) {
PersonRequest req = FaceOrgFactory.getInstance().requestFactory().personRequest();
PersonProxy eObject = req.edit(object);
eObject.setName(value);
req.persist().using(eObject).fire();
}
});
and my code for data provider:
AsyncDataProvider<PersonProxy> personDataProvider = new AsyncDataProvider<PersonProxy>() {
#Override
protected void onRangeChanged(HasData<PersonProxy> display) {
final Range range = display.getVisibleRange();
fetch(range.getStart());
}
};
personDataProvider.addDataDisplay(personTable);
...
private void fetch(final int start) {
lastFetch = start;
requestFactory.personRequest().getPeople(start, numRows).fire(new Receiver<List<PersonProxy>>() {
#Override
public void onSuccess(List<PersonProxy> response) {
if (lastFetch != start){
return;
}
int responses = response.size();
if (start >= (personTable.getRowCount()-numRows)){
PersonProxy newP = requestFactory.personRequest().create(PersonProxy.class);
response.add(newP);
responses++;
}
personTable.setRowData(start, response);
personPager.setPageStart(start);
}
});
requestFactory.personRequest().countPersons().fire(new Receiver<Integer>() {
#Override
public void onSuccess(Integer response) {
personTable.setRowCount(response+1, true);
}
});
}
I try to insert last object a new empty object. And when user would fill it, I'd insert new one after it. But the code is not working. I says that user is "attempting" to edit a object previously edited by another RequestContext.
Dilemmas:
* am I creating too many context'es?
* how to properly insert new object into celltable, created on the client side?
* on fieldUpdater when I get an editable object - should I insert it back to table or forget about it?
Thanks for any help.
am I creating too many context'es?
Yes.
You should have one context per HTTP request (per fire()), and a context that is not fire()d is useless (only do that if you/the user change your/his mind and don't want to, e.g., save your/his changes).
You actually have only one context to remove here (see below).
Note that your approach of saving on each field change can lead to "race conditions", because a proxy can be edit()ed by at most one context at a time, and it remains attached to a context until the server responds (and once a context is fired, the proxy is frozen –read-only– also until the server responds).
(this is not true in all cases: when onConstraintViolation is called, the context and its proxies are unfrozen so you can "fix" the constraint violations and fire the context again; this should be safe because validation is done on the server-side before any service method is called).
how to properly insert new object into celltable, created on the client side?
Your code looks OK, except that you should create your proxy in the same context as the one you'll use to persist it.
on fieldUpdater when I get an editable object - should I insert it back to table or forget about it?
I'm not 100% certain but I think you should refresh the table (something like setRowData(index, Collections.singletonList(object)))
BTW, the driver people mention is probably the RequestFactoryEditorDriver from the Editor framework. It won't help you here (quite the contrary actually).

GWT column sort handler ,how to get column's value which has been selected

I have a celltable in GWT and want to implement sorting functionality on it , from database(Criteria)
for that i just want to know how to get the value of the column which has been clicked for sorting
here is my code
ctJobs.addColumnSortHandler(new ColumnSortEvent.Handler() {
public void onColumnSort(ColumnSortEvent event) {
event.getColumn();
event.getColumn().getValue("what do we need to write here ???");
from event.getColumn() , i am getting column in the form of object
com.google.gwt.cell.client.ClickableTextCell#188a12e
I want to know the the column's name / value
for that i am trying event.getcolumn().getvalue("??");
but what is the parameter for that, or is there any other way of getting column's name which has been clicked.
Thanks
Are you using a ListDataProvider or an AsyncDataProvider for your cell table?
In case of an AsyncDataProvider the sorting must be done on the server side, so there is no need to add a ColumnSortHandler.
Please see the GWT docs.
To get the name of the column clicked for sorting see this question.
When creating the table columns, set the dataStoreName of the column.
column.setDataStoreName("columnX");
Next, when in the AsyncDataProvider get the sort history of the clicked headers like the following
final AsyncDataProvider<SQLRow> dataProvider = new AsyncDataProvider<SQLRow>(){
#Override
protected void onRangeChanged(HasData<SQLRow> display) {
for (int i=0;i<sortList.size();i++) {
sortList.get(i).getColumn().getDataStoreName();
}
}
}

How to get the affected table name from DataSet, SQLDataAdapter or SQLCommandBuilder?

I am doing a simple connect, fill, update operation using an SQLConnector, an SQLCommandBuilder and a DataSet.
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "server=scratch;database=scratch;user=scratch;password=scratch";
theConnection = new SqlConnection(connectionString);
theDataAdapter = new SqlDataAdapter("SELECT * FROM scratch", theConnection);
theCommandBuilder = new SqlCommandBuilder(theDataAdapter);
theDataSet = new DataSet();
theDataAdapter.Fill(theDataSet);
dataGridView1.DataSource = theDataSet.Tables[0];
}
private void button2_Click(object sender, EventArgs e)
{
theDataAdapter.Update(theDataSet.Tables[0]);
}
I want to log the name of the table, but the TableName of the tables in the dataset is always 'Table'. The command builder uses the table name to create the Update, Insert and Delete commands but I can't find a method or property of SQLCommandBulider or DBCommandBuilder that returns it.
For maintainablilty's sake I'd prefer to keep the changes in the second event. So I don't want to require anything additional when setting up the DataAdatpter or DataSet (such as explicitly setting the DataTables' TableNames). Parsing the output of GetUpdateCommand() also just sounds kludgy.
What is the neat, correct way to get the name of the affected database table at update time?
... or am I just being too picky about what I don't want to do?