我从我的教程中得到了一些问题,我正在从我的android书中做,我已经开始用android studio程序学习它了一段时间。 我已经厌倦了使用SQLite的应用程序,
但是当我完成代码并尝试运行它时,我从“错误日志”中收到此消息。
11-17 18:05:19.905 1314-1771/? E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xa0f86a60
11-17 18:06:07.027 2470-2470/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
11-17 18:06:07.027 2470-2470/? E/android.os.Debug﹕ failed to load memtrack module: -2
11-17 18:06:07.060 2470-2478/? E/art﹕ Thread attaching while runtime is shutting down: Binder_1
11-17 18:06:07.722 2481-2481/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
11-17 18:06:07.722 2481-2481/? E/android.os.Debug﹕ failed to load memtrack module: -2
11-17 18:06:07.983 2336-2354/? E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xabf10560
11-17 18:06:07.997 2490-2490/? E/SQLiteLog﹕ (1) near "TABLEnotes": syntax error
11-17 18:06:07.998 2490-2490/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sweetpengiuns.simplenote, PID: 2490
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sweetpengiuns.simplenote/com.sweetpengiuns.simplenote.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEnotes": syntax error (code 1): , while compiling: CREATE TABLEnotes(_idINTEGER PRIMARY KEY AUTOINCREMENT, timeINTEGER,contentTEXT NOT NULL);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: android.database.sqlite.SQLiteException: near "TABLEnotes": syntax error (code 1): , while compiling: CREATE TABLEnotes(_idINTEGER PRIMARY KEY AUTOINCREMENT, timeINTEGER,contentTEXT NOT NULL);
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 com.sweetpengiuns.simplenote.NotesHelper.onCreate(NotesHelper.java:25)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.sweetpengiuns.simplenote.MainActivity.getAllNotes(MainActivity.java:75)
at com.sweetpengiuns.simplenote.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
11-17 18:06:08.026 986-986/? E/EGL_emulation﹕ tid 986: eglCreateSyncKHR(1243): error 0x3004 (EGL_BAD_ATTRIBUTE)
11-17 18:06:10.260 1314-1771/? E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xa0f86a60
这是我的代码:
- Constants.java
package com.sweetpengiuns.simplenote;
import android.provider.BaseColumns;
public interface Constants extends BaseColumns{
public static final String TABLE_NAME = "notes";
public static final String TIME = "time";
public static final String CONTENT = "content";
}
- MainActivity.java
package com.sweetpengiuns.simplenote;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Date;
import static com.sweetpengiuns.simplenote.Constants.CONTENT;
import static com.sweetpengiuns.simplenote.Constants.TABLE_NAME;
import static com.sweetpengiuns.simplenote.Constants.TIME;
import static com.sweetpengiuns.simplenote.Constants._ID;
public class MainActivity extends AppCompatActivity {
private NotesHelper helper;
private static String[] COLUMNS = { _ID, TIME, CONTENT };
private static String ORDER_BY = TIME +" DESC";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new NotesHelper(this);
try{
Cursor cursor = getAllNotes();
showNotes(cursor);
}
finally{
helper.close();
}
final EditText txtNewText = (EditText) findViewById(R.id.new_text);
Button btnSave = (Button) findViewById(R.id.save_button);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try{
addNote(txtNewText.getText().toString());
Cursor cursor = getAllNotes();
showNotes(cursor);
txtNewText.setText(null);
}
finally{
helper.close();
}
}
});
}
private void addNote(String str){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TIME, System.currentTimeMillis());
values.put(CONTENT,str);
db.insertOrThrow(TABLE_NAME, null, values);
}
private Cursor getAllNotes(){
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null, ORDER_BY);
return cursor;
}
private void showNotes(Cursor cursor){
StringBuilder builder = new StringBuilder("Messages has recorded:\n\n");
while(cursor.moveToNext()){
long id = cursor.getLong(0);
long time = cursor.getLong(1);
String content = cursor.getString(2);
builder.append("Number").append(id).append(": ");
String strDate = (String) DateFormat.format("yyyy-MM-dd hh:mm:ss",new Date(time));
builder.append(strDate).append("\n");
builder.append("\t").append(content).append("\n");
}
TextView tv = (TextView) findViewById(R.id.all_text);
tv.setText(builder);
}
}
**- NotesHelper.java**
package com.sweetpengiuns.simplenote;
/**
* Created by Paradorn on 16/11/2558.
*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static com.sweetpengiuns.simplenote.Constants.CONTENT;
import static com.sweetpengiuns.simplenote.Constants.TABLE_NAME;
import static com.sweetpengiuns.simplenote.Constants.TIME;
import static com.sweetpengiuns.simplenote.Constants._ID;
public class NotesHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME ="simple_note.db";
private static final int DATABASE_VERSION = 1;
public NotesHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE" + TABLE_NAME + "(" + _ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME + "INTEGER,"
+ CONTENT + "TEXT NOT NULL);");
}
}
- activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dip">
<EditText
android:id="@+id/new_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<Button
android:id="@+id/save_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Save"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dip"
android:orientation="vertical">
<TextView
android:id="@+id/all_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
</ScrollView>
</LinearLayout>
我试图在我的代码中多次查找错误,但我找不到它,我的编程也不强。这是我第一个问你的问题,这似乎是个愚蠢的问题,但你可以建议我做任何事情。
最后,我从编码中发现了错误,这是我的愚蠢。
这段代码使我的应用程序出错::
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE" + TABLE_NAME + "(" + _ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME + "INTEGER,"
+ CONTENT + "TEXT NOT NULL);");
}
因为我在代码中没有注意到我写的SQL命令中的变量类型之间是否没有空格。
我修复了这段代码:
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME + " INTEGER,"
+ CONTENT + " TEXT NOT NULL);");
}
答案 0 :(得分:1)
完成光标后,需要将其关闭:
cursor.close();