gpt4 book ai didi

android - SQLiteDatabase.execSQL() 不工作。它给我消息 "Non-static method ' execSQL' cannot be a reference from a static context"

转载 作者:搜寻专家 更新时间:2023-10-30 22:26:57 25 4
gpt4 key购买 nike

我正在尝试在 Android SQLite 数据库中创建一个表。但是 execSQLite 方法在 on create 方法中不起作用。它给了我信息-

Non-static method 'execSQL' cannot be a reference from a static context

但是我调用的方法是一个非静态方法。下面给出方法。

  public void onCreate(SQLiteDatabase sqLiteDatabase) {
SQLiteDatabase.execSQL();
}

这是我的完整 java 代码:

package com.sarsinetvarneshon.basicdb;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Sarsinet Varneshon on 23-Apr-18.
*/

public class EmployeeDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "db_employee";
public static final int VERSION = 1;
public static final String EMPLOYEE_TABLE_NAME = "tbl_employee";

public static final String EMPLOYEE_COL_ID = "id";
public static final String EMPLOYEE_COL_NAME = "name";
public static final String EMPLOYEE_COL_AGE = "age";
public static final String EMPLOYEE_COL_DEPT = "dept";

public static final String CREATE_TABLE_EMPLOYEE = " CREATE TABLE "+ EMPLOYEE_TABLE_NAME+
"("+EMPLOYEE_COL_ID+ " INTEGER PRIMARY KEY, "+
EMPLOYEE_COL_NAME+ " TEXT, "+
EMPLOYEE_COL_AGE+ " INT, "+
EMPLOYEE_COL_DEPT+ " TEXT)";




public EmployeeDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
SQLiteDatabase.execSQL();

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

最佳答案

改变行:

  SQLiteDatabase.execSQL();

到这一行:

  sqLiteDatabase.execSQL();

您使用的是类名而不是变量。

关于android - SQLiteDatabase.execSQL() 不工作。它给我消息 "Non-static method ' execSQL' cannot be a reference from a static context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988817/

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