gpt4 book ai didi

java - 在Android异常中插入SQLite

转载 作者:行者123 更新时间:2023-12-01 22:29:13 25 4
gpt4 key购买 nike

我创建了一个简单的 Android 应用程序来处理 SQLite 数据库,以便从表中插入和检索数据,但是当我尝试插入数据或检索时,会发生如下所示的异常:

01-26 15:48:30.930: E/AndroidRuntime(25783): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.loginandregister/com.androidhive.loginandregister.LoginActivity}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: INSERT INTO info() VALUES (?)

下面是我的代码:

LoginActivity.java

package com.androidhive.loginandregister;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends Activity {
DataHandler data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

data= new DataHandler(getBaseContext());
data.open();
long x = data.insertdata("a", "b", "c", "d", "e");

Toast.makeText(getBaseContext(),"Insertion Completed", Toast.LENGTH_LONG).show();

data.close();

Button btn = (Button)findViewById(R.id.btnLogin);

btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

data = new DataHandler(getBaseContext());
data.open();
Cursor c = data.returndata();
if(c.moveToFirst())
{
do
{
Toast.makeText(getBaseContext(),c.getString(0)+ " "+ c.getString(1)+" "+c.getString(2) ,Toast.LENGTH_LONG).show();
}while(c.moveToNext());


}
data.close();
}

});

}
}

DataHandler.java

package com.androidhive.loginandregister;

import android.content.ContentValues;
import android.content.Context;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHandler {

public static final String id ="";
public static final String name ="";
public static final String number ="";
public static final String email ="";
public static final String website ="";
public static final String table_name ="info";
public static final String data_base_name ="bcb";
public static final int database_version = 1;
public static final String table_create ="CREATE TABLE info(id TEXT,name TEXT,number TEXT,email TEXT,website TEXT);";

databasehelper dbhelper;
Context ctx;
SQLiteDatabase db;


public DataHandler (Context ctx)
{
this.ctx=ctx;
dbhelper = new databasehelper(ctx);
}

private static class databasehelper extends SQLiteOpenHelper
{

public databasehelper(Context ctx)
{
super(ctx,data_base_name,null,database_version);
}

@Override
public void onCreate(SQLiteDatabase db) {

try
{
db.execSQL(table_create);
}

catch (SQLException e)
{
e.printStackTrace();
}

}




@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS info");
onCreate(db);
}

}


public DataHandler open()
{
db = dbhelper.getWritableDatabase();
return this;
}

public void close()
{
dbhelper.close();

}

public long insertdata(String ID, String NAME,String NUMBER,String EMAIL,String WEBSITE,String COMPANY,String PROFESSION,String COMMENTS,String STATUS)
{
ContentValues content = new ContentValues();
content.put(id, ID);
content.put(name, NAME);
content.put(number,NUMBER);
content.put(email, EMAIL);
content.put(website, WEBSITE);
return db.insertOrThrow(table_name, null, content);


}

public Cursor returndata()
{
return db.query(table_name, new String [] {id,name,number,email,website}, null, null, null, null, null);
}
}

感谢您的帮助。

最佳答案

我认为这些应该包含您的列名称

public static final String id ="";
public static final String name ="";
public static final String number ="";
public static final String email ="";
public static final String website ="";

这就是为什么您在插入查询中得到空列列表的原因。用适当的值填充它们。

关于java - 在Android异常中插入SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com