Android sql错误:E/SQLiteLog:(1)接近“null”:语法错误。

时间:2022-11-02 23:04:49

I'm learning to work with Database in Android. Can't seem to get it to work. I have provided the Log (at the end) to help you understand better.

我正在学习使用Android的数据库。似乎无法让它发挥作用。我已经提供了日志(在最后)以帮助您更好地理解。

Here's my dbHadler.java

这是我的dbHadler.java

package todo.do2.com.todo;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class dbHandler extends SQLiteOpenHelper{

//VERSION
private static final int DATABASE_VERSION = 1;

//NAME
private static final String DATABASE_NAME = "Mydb";

//DATABASE TABLE NAME
private static final String TABLE_LIST = "Mylist";

//TABLE COLUMN NAMES
private static final String KEY_ID = "id";
private static final String KEY_ListItem = "listitems";

public dbHandler(Context context) {
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

//CREATING TABLES
@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID + " INTEGER," + KEY_ListItem + " TEXT" + ")";

    db.execSQL(CREATE_LIST_TABLE);
}

//UPGRADING DATABASE
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion){
    //drop old table if exist
    db.execSQL("DROP TABLE IF EXIST" + TABLE_LIST);

    onCreate(db);
}

void addListItem(ArrayList<String> listitems) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    for (int i = 1; i < listitems.size(); i++)
    {
        Log.e("Value inserting == ","" + listitems.get(i));

        db.insert(TABLE_LIST,null,values);
    }

    db.close(); //closing database connection
}

Cursor getListItem() {

    String selectQuery = "SELECT * FROM " + TABLE_LIST;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery,null);

    return cursor;
}
}

Here's my Activity_Main.java

这是我的Activity_Main.java

package todo.do2.com.todo;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import android.database.Cursor;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

FloatingActionButton btn;
ListView list;
EditText editText;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.edit);
    btn = (FloatingActionButton) findViewById(R.id.addBtn);
    list = (ListView) findViewById(R.id.list);
    listItems = new ArrayList<>();


    listItems.add("Welcome");
    listItems.add("second");


    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listItems);

    list.setAdapter(adapter);

            dbHandler db = new dbHandler(this);

    db.addListItem(listItems);


    Cursor cursor = db.getListItem();

    Log.e("count", " " + cursor.getCount());
    if(cursor != null)
    {
        cursor.moveToNext();

        do{
            Log.e("values == ", "" + cursor.getString(1));
        }while(cursor.moveToNext());
    }

}
}

Here's my activity_main.xml

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingEnd="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_vertical_margin"
android:isScrollContainer="false">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/addBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginLeft="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginBottom="12dp"
    android:clickable="true"
    android:onClick="addItems"
    app:fabSize="normal"
    android:isScrollContainer="true"/>


<EditText
    android:id="@+id/edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="18dp"
    android:layout_marginBottom="12dp"
    android:layout_toLeftOf="@+id/addBtn"
    android:hint="Add New Item"
    android:isScrollContainer="true"/>


<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/edit"
    android:drawSelectorOnTop="false"
    android:scrollbars="none"
    android:isScrollContainer="false"/>

Here's the generated Log

这是生成的日志

E/Value inserting ==: second
E/SQLiteLog: (1) near "null": syntax error
E/SQLiteDatabase: Error inserting 
              android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO Mylist(null) VALUES (NULL)
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                  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.insertWithOnConflict(SQLiteDatabase.java:1469)
                  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                  at todo.do2.com.todo.dbHandler.addListItem(dbHandler.java:56)
                  at todo.do2.com.todo.MainActivity.onCreate(MainActivity.java:70)
                  at android.app.Activity.performCreate(Activity.java:6251)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5461)
                  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)
                  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
E/count:  0

1 个解决方案

#1


1  

I think here is the problem

我想这就是问题所在。

 ContentValues values = new ContentValues();
for (int i = 1; i < listitems.size(); i++)
{
    Log.e("Value inserting == ","" + listitems.get(i));

    db.insert(TABLE_LIST,null,values);
}

the values is always null

值总是为null。

try this

试试这个

for (int i = 1; i < listitems.size(); i++)
{
    Log.e("Value inserting == ","" + listitems.get(i));
    values.put(KEY_ID,i);
    values.put(KEY_ListItem,listitems.get(i));
    db.insert(TABLE_LIST,null,values);
}

#1


1  

I think here is the problem

我想这就是问题所在。

 ContentValues values = new ContentValues();
for (int i = 1; i < listitems.size(); i++)
{
    Log.e("Value inserting == ","" + listitems.get(i));

    db.insert(TABLE_LIST,null,values);
}

the values is always null

值总是为null。

try this

试试这个

for (int i = 1; i < listitems.size(); i++)
{
    Log.e("Value inserting == ","" + listitems.get(i));
    values.put(KEY_ID,i);
    values.put(KEY_ListItem,listitems.get(i));
    db.insert(TABLE_LIST,null,values);
}