我用外键创建了一个表,它在执行中产生了一个错误,我无法理解错误如下......
01-11 08:57:28.821 27100-27100/company.viral.organizadorjec E/AndroidRuntime: FATAL EXCEPTION: main
Process: company.viral.organizadorjec, PID: 27100
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18798)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18798)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "foreing": syntax error (code 1): , while compiling: create table materias (id integer primary key autoincrement, nombre text, id_profesor integer, id_periodo integer, detalle text, foreign key(id_profesor) references profesores (id),foreing key(id_periodo) references periodo(id))
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1697)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1628)
at company.viral.organizadorjec.ActivitysPrincipales.SQLite.onCreate(SQLite.java:31)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at company.viral.organizadorjec.ActivitysPrincipales.MainActivity.onClickAcepta(MainActivity.java:49)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4472)
at android.view.View$PerformClick.run(View.java:18798)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
以下是涉及方法的类。
数据库类。
SQLite.java
package company.viral.organizadorjec.ActivitysPrincipales;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by erny on 27/10/2016.
*/
public class SQLite extends SQLiteOpenHelper {
//constructor.......
public SQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//aqui se crea la tabla...
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table usuarios (id integer primary key autoincrement, " +
"nombre text, clave text)");
db.execSQL("create table profesores (id integer primary key autoincrement, " +
"nombre text, detalle text)");
db.execSQL("create table materias (id integer primary key autoincrement, " +
"nombre text, id_profesor integer, id_periodo integer, detalle text, " +
"foreign key(id_profesor) references profesores (id)," +
"foreing key(id_periodo) references periodo(id))");
db.execSQL("create table periodo (id integer primary key autoincrement, " +
"nombre text, id_usuario integer, fechainicio integer, fechacierre integer," +
"foreing key(id_usuario) references usuarios(id))");
db.execSQL("create table caracteristicas (id integer primary key autoincrement, " +
"nombre text)");
db.execSQL("create table asignacion (id integer primary key autoincrement, " +
"detalle text, id_materia integer, fecha integer, id_periodo integer, id_tarea integer," +
"foreing key(id_materia) references materias(id)," +
"foreing key(id_periodo) references periodo(id)," +
"foreing key(id_tarea) references tarea(id)");
db.execSQL("create table tarea (id integer primary key autoincrement, " +
"nombre text)");
db.execSQL("create table caracteristica_profesor (id_profesor integer, id_caracteristica integer," +
"foreing key(id_profesor) references profesores(id)," +
"foreing key(id_caracteristica) references caracteristicas(id)");
db.execSQL("insert into usuarios values('0','admin','admin')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("create table usuarios (id integer primary key autoincrement, " +
"nombre text, clave text)");
db.execSQL("create table profesores (id integer primary key autoincrement, " +
"nombre text, detalle text)");
db.execSQL("create table materias (id integer primary key autoincrement, " +
"nombre text, id_profesor integer, id_periodo integer, detalle text, " +
"foreign key(id_profesor) references profesores (id)," +
"foreing key(id_periodo) references periodo(id))");
db.execSQL("create table periodo (id integer primary key autoincrement, " +
"nombre text, id_usuario integer, fechainicio integer, fechacierre integer," +
"foreing key(id_usuario) references usuarios(id))");
db.execSQL("create table caracteristicas (id integer primary key autoincrement, " +
"nombre text)");
db.execSQL("create table asignacion (id integer primary key autoincrement, " +
"detalle text, id_materia integer, fecha integer, id_periodo integer, id_tarea integer," +
"foreing key(id_materia) references materias(id)," +
"foreing key(id_periodo) references periodo(id)," +
"foreing key(id_tarea) references tarea(id)");
db.execSQL("create table tarea (id integer primary key autoincrement, " +
"nombre text)");
db.execSQL("create table caracteristica_profesor (id_profesor integer, id_caracteristica integer," +
"foreing key(id_profesor) references profesores(id)," +
"foreing key(id_caracteristica) references caracteristicas(id)");
db.execSQL("insert into usuarios values('0','admin','admin')");
}
}
MainActivity.java
package company.viral.organizadorjec.ActivitysPrincipales;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import company.viral.organizadorjec.R;
//aqui empieza...
public class MainActivity extends AppCompatActivity {
//creamos variables EditText para capturar los datos
private EditText aetid,aetpass;
private Cursor fila;
//en este metodo SIEMPRE se dibuja la app correspondiente
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//antes de dibujar definimos las variables y a quienes pertecen en el layout
aetid = (EditText) findViewById(R.id.etid);
aetpass = (EditText) findViewById(R.id.etpass);
}
//creamos los metodos con los que reaccionan los btn (onClick)
/*metodo para entrar y buscar (en construccion.... explorando metodos)*/
public void onClickAcepta (View view) {
String auxn = aetid.getText().toString();
String auxp = aetpass.getText().toString();
SQLite admin = new SQLite(this,"administracion", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase();
fila=bd.rawQuery("select nombre, clave from usuarios where nombre='"+auxn+"'and clave='"+auxp+"'",null);
if(fila.moveToFirst()==true){
//capturamos los valores del cursos y lo almacenamos en variable
String usua=fila.getString(0);
String pass=fila.getString(1);
//preguntamos si los datos ingresados son iguales
if (auxn.equals(usua)&&auxp.equals(pass)){
//si son iguales entonces vamos a otra ventana
//Menu es una nueva actividad empty
Intent ven=new Intent(this,MenuCentral.class);
startActivity(ven);
//limpiamos las las cajas de texto
aetid.setText("");
aetpass.setText("");
finish();
}
}else {
Toast.makeText(getApplicationContext(), "Usuario o contraseña erroneo", Toast.LENGTH_LONG).show();
}
bd.close();
}
//metodo para entrar a la actividad de registro
public void onClickRegistro(View view){
Intent i = new Intent(this,Registro.class);
startActivity(i);
finish();
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("¿Desea Salir de la Aplicación?");
builder.setTitle("Alerta!");
builder.setPositiveButton("SI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
}
查看您的错误消息时,您会发现:
首先
foreing
通过foreign
http://www.w3schools.com/sql/sql_foreignkey.asp
当您尝试使用它执行脚本时,
db.execSQL()
它无法识别该单词,因此会生成错误。