我正在使用Android Studio编写应用程序。它是一个简单的练习应用程序。 我确信它会起作用,但不会。出了点问题。 我将在此处粘贴我的代码:
package programowanie.android.sql;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper db;
EditText editTextImie;
EditText editTextNazwisko;
EditText editTextTelefon;
EditText editTextEmail;
EditText editTextUlica;
EditText editTextKod;
Button btDodaj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
editTextImie = (EditText) findViewById(R.id.editTextImie);
editTextNazwisko = (EditText) findViewById(R.id.editTextNazwisko);
editTextTelefon = (EditText) findViewById(R.id.editTextTelefon);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextUlica = (EditText) findViewById(R.id.editTextUlica);
editTextKod = (EditText) findViewById(R.id.editTextKod);
btDodaj = (Button) findViewById(R.id.btDodaj);
btDodaj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean czysieudalo;
czysieudalo = db.wstawdane(editTextImie.getText().toString(), editTextNazwisko.getText().toString(), editTextTelefon.getText().toString(), editTextEmail.getText().toString(), editTextUlica.getText().toString(), editTextKod.getText().toString());
if(czysieudalo){
Toast.makeText(MainActivity.this, "Udało się!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Niestety nie udało się :(!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
这是我的DatabaseHelper类:
package programowanie.android.sql;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Kudłaty on 2016-02-19.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String database_name ="Kontakty";
public static final String database_table ="Osoby";
public DatabaseHelper(Context context) {
super(context, database_name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + database_table + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST" + database_table);
onCreate(db);
}
public boolean wstawdane(String imie, String nazwisko, String nr_tel, String email, String ulica, String kod ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("Imie", imie);
cv.put("Nazwisko", nazwisko);
cv.put("Nr telefonu", nr_tel);
cv.put("Email", email);
cv.put("Ulica", ulica);
cv.put("Kod pocztowy", kod);
if (db.insert(database_table, null, cv)==-1){
return false;
} else {
return true;
}
}
}
我没有错误,但是当我尝试添加Record app停止时,我得到了#34; SQL已停止"
我忘了粘贴日志!你在这里!
02-19 18:02:43.119 2087-2087/programowanie.android.sql I/art: Not late-enabling -Xcheck:jni (already on)
02-19 18:02:43.195 2087-2087/programowanie.android.sql W/System: ClassLoader referenced unknown path: /data/app/programowanie.android.sql-1/lib/x86
02-19 18:02:43.348 2087-2115/programowanie.android.sql D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-19 18:02:43.410 2087-2115/programowanie.android.sql I/OpenGLRenderer: Initialized EGL, version 1.4
02-19 18:02:43.544 2087-2115/programowanie.android.sql W/EGL_emulation: eglSurfaceAttrib not implemented
02-19 18:02:43.544 2087-2115/programowanie.android.sql W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdff300, error=EGL_SUCCESS
02-19 18:03:19.132 2087-2087/programowanie.android.sql E/SQLiteLog: (1) near "tableOsoby": syntax error
02-19 18:03:19.133 2087-2087/programowanie.android.sql D/AndroidRuntime: Shutting down VM
02-19 18:03:19.133 2087-2087/programowanie.android.sql E/AndroidRuntime: FATAL EXCEPTION: main
Process: programowanie.android.sql, PID: 2087
android.database.sqlite.SQLiteException: near "tableOsoby": syntax error (code 1): , while compiling: create tableOsoby(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at programowanie.android.sql.DatabaseHelper.onCreate(DatabaseHelper.java:21)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at programowanie.android.sql.DatabaseHelper.wstawdane(DatabaseHelper.java:31)
at programowanie.android.sql.MainActivity$1.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:3)
更改此
{
foo: true,
associated: {
bar: true
}
}
到这个
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + database_table + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT");
}
'table和(ID'之间没有空格所以你的命令就是
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + database_table + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT);");
}
哪个不会跑。
我也注意到你忘记了)你已经忘记了;在声明的最后。你必须记住;出于安全原因
答案 1 :(得分:1)
给出错误日志:
android.database.sqlite.SQLiteException:near&#34; tableOsoby&#34 ;:语法错误(代码1):,同时编译:create tableOsoby(ID INTEGER PRIMARY KEY AUTOINCREMENT,IMIE TEXT,NAZWISKO TEXT,NUMER_TELEFONU TEXT,电子邮件文本,ULICA TEXT,KOD_MIASTO TEXT
我在这里发现了两个可能导致SQLite崩溃的问题:
1)在db.execSQL("create table" + database_table + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT");
行上,它在&#34;创建表&#34;之间缺少一个空格。字符串和表名变量,导致&#34;创建tableOsoby&#34;。与下一个连接字符串相同。
2)字符串"(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT"
中缺少右括号。
更正后的行应为:
db.execSQL("create table " + database_table + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT)");