При попытке использовать query вылетает ошибка:
Код: Выделить всё
Failed to get GED Log Buf, err(0)
12-23 08:05:24.461 32630 32630 D mark TABLE create table myTab (id integer primary key autoincrement,forText text,forNums integer); created
12-23 08:06:43.786 32630 32630 E SQLiteLog (1) near "FROM": syntax error
12-23 08:06:43.794 32630 32630 E AndroidRuntime FATAL EXCEPTION: main
12-23 08:06:43.794 32630 32630 E AndroidRuntime Process: com.mycompany.myapp, PID: 32630
12-23 08:06:43.794 32630 32630 E AndroidRuntime android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT FROM myTab
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1193)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1064)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1232)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at com.mycompany.myapp.MainActivity.onClick(MainActivity.java:130)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.view.View.performClick(View.java:4848)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.view.View$PerformClick.run(View.java:20262)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.os.Handler.handleCallback(Handler.java:815)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:104)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.os.Looper.loop(Looper.java:194)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5631)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:372)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
12-23 08:06:43.794 32630 32630 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
MainActivity.java
Код: Выделить всё
[syntax=java5]
package com.mycompany.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.*;
import android.util.*;
import android.widget.*;
import android.webkit.*;
import android.net.*;
import android.database.sqlite.*;
import android.database.*;
public class MainActivity extends Activity implements OnClickListener
{
final String tag="mark";
Tabuild Tad;
String tapamS,nameS,colS,datS,qolS[],selS,argS[],havS,gruS,ordS;
Button creBUT,inBUT,upBUT,delBUT,clerBUT,qurBUT;
EditText tapamET,namET,colET,datET,qolET,selET,argET,havET,gruET,ordET;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Tad = new Tabuild(this);
creBUT = (Button)findViewById(R.id.cretab);
inBUT = (Button)findViewById(R.id.insert);
upBUT = (Button)findViewById(R.id.update);
delBUT = (Button)findViewById(R.id.delete);
clerBUT = (Button)findViewById(R.id.clear);
qurBUT = (Button)findViewById(R.id.query);
creBUT.setOnClickListener(this);
inBUT.setOnClickListener(this);
upBUT.setOnClickListener(this);
delBUT.setOnClickListener(this);
clerBUT.setOnClickListener(this);
qurBUT.setOnClickListener(this);
}
public void onClick(View p1)
{
qolS=new String[1];
argS=new String[1];
tapamS=nameS=colS=datS=qolS[0]=selS=argS[0]=havS=gruS=ordS=null;
tapamET = (EditText)findViewById(R.id.tabpam);
namET = (EditText)findViewById(R.id.TabNameET);
colET = (EditText)findViewById(R.id.colname);
datET = (EditText)findViewById(R.id.data);
qolET = (EditText)findViewById(R.id.colms);
selET = (EditText)findViewById(R.id.select);
argET = (EditText)findViewById(R.id.selargs);
havET = (EditText)findViewById(R.id.have);
gruET = (EditText)findViewById(R.id.group);
ordET = (EditText)findViewById(R.id.order);
tapamS = tapamET.getText().toString();
nameS = namET.getText().toString();
colS = colET.getText().toString();
datS = datET.getText().toString();
qolS[0] = qolET.getText().toString();
selS = selET.getText().toString();
argS[0] = argET.getText().toString();
havS = havET.getText().toString();
gruS = gruET.getText().toString();
ordS = ordET.getText().toString();
ContentValues CV=new ContentValues();
Cursor CSR;
SQLiteDatabase sqlid=Tad.getWritableDatabase();
switch (p1.getId())
{
case R.id.cretab:
{
sqlid.execSQL(tapamS);
Log.d(tag, "TABLE " + tapamS + " created");
Toast.makeText(this, "new table created", Toast.LENGTH_SHORT).show();
}
break;
case R.id.insert:
{
CV.put(colS, datS);
sqlid.insert(nameS, null, CV);
Toast.makeText(this,datS+" in "+colS+" inserted",Toast.LENGTH_SHORT).show();
}
break;
case R.id.update:
{
CV.put(colS, datS);
sqlid.update(nameS, CV, selS, argS);
}
break;
case R.id.delete:
{
sqlid.delete(nameS, selS, argS);
}
break;
case R.id.clear:
{
tapamET.setText(null);
namET.setText( null);
colET.setText( null);
datET.setText( null);
qolET.setText( null);
selET.setText( null);
argET.setText( null);
havET.setText( null);
gruET.setText( null);
ordET.setText( null);
}
break;
case R.id.query:
{
String res="--";
CSR = sqlid.query(nameS, qolS, selS, argS, havS, gruS, ordS);
if (CSR.moveToFirst())
{
String colnam[]= CSR.getColumnNames();
for (String cn:colnam)
{
res += cn + "--";
}
Log.d(tag, res);
res = "--";
do
{
for (String cn:colnam)
{
int colind=CSR.getColumnIndex(cn);
if((CSR.getType(colind))==CSR.FIELD_TYPE_STRING)
res += (CSR.getString(colind)) + "--";
else if((CSR.getType(colind))==CSR.FIELD_TYPE_INTEGER)
res+=(CSR.getInt(colind))+"--";
}
Log.d(tag, res);
res = "--";
}
while(CSR.moveToNext());
}
else Toast.makeText(this,"CSR is empty",Toast.LENGTH_SHORT).show();
CSR.close();
Tad.close();
}
break;
}
}
class Tabuild extends SQLiteOpenHelper
{
Tabuild(Context Cxt)
{
super(Cxt, "DaB", null, 1);
}
public void onCreate(SQLiteDatabase p1)
{
}
public void onUpgrade(SQLiteDatabase p1, int p2, int p3)
{
}
}
}
[/syntax]
main.xml
Код: Выделить всё
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="create"
android:id="@+id/cretab"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabpam"/>
</LinearLayout>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="TabName:"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/TabNameET"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="column (cv): "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/colname"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="data (cv): "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/data"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:layout_marginTop="10dp"
android:text="insert"
android:id="@+id/insert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:layout_marginTop="10dp"
android:text="update"
android:id="@+id/update"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="columns: "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/colms"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="selection: "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/select"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="ARGS[]: "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/selargs"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:layout_marginTop="10dp"
android:text="delete"
android:id="@+id/delete"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:layout_marginTop="10dp"
android:text="clear"
android:id="@+id/clear"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:text="having: "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/have"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="group: "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/group"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="order: "
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:id="@+id/order"
android:minWidth="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button
android:layout_marginTop="10dp"
android:text="query"
android:id="@+id/query"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</TableLayout>
</TableRow>
</LinearLayout>
При вводе аргументов для query в коде , например: query(myTab,null,null,null,null,null,null) -всё работает , но если брать аргументы из полей ввода , при работе самого приложения возникает ошибка -подозреваю что дело в массивах columns и selectARGS , так как если значения для них в параметрах query выставить null прямо в коде , то значения для остальных параметров нормально берутся из полей и программа работает