I am getting this, actually its running in an old version but not running in Android 2.2 this is the code... I don't what to replace it with or have an alternative. So i have added the whole code to be able to understand the real. the problem i have seen it run on an older version of android studio.
我得到了这个,实际上它运行在一个旧版本但没有在Android 2.2中运行这是代码...我不知道要替换它或有替代品。所以我添加了整个代码,以便能够理解真实。问题我看到它运行在旧版本的Android工作室。
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String Database_Name= "student.db";
public static final String Table_Name= "student_table";
public static final String COL_1= "id";
public static final String COL_2= "name";
public static final String COL_3= "surname";
public static final String COL_4= "marks";
public DatabaseHelper(Context context){
super(context, Database_Name,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + Table_Name + " (id integer primary key auto increment, name text, surname text, marks ineteger)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists" + Table_Name);
onCreate(db);
}
public boolean insertData(String name, String surname, String marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
ContentValues.put(COL_2,name);
ContentValues.put(COL_3,surname);
ContentValues.put(COL_4,marks);
long result = db.insert(Table_Name, null,contentValues);
if (result==-1) {
return false;
} else {
return true;
}
}
}
1 个解决方案
#1
0
This error happens because you are mixing up static and non-static parts of your class.
发生此错误的原因是您混合了类的静态和非静态部分。
Take this example:
举个例子:
public class Test {
public String testVariable = "Honk";
public static String methodTest() {
return testVariable;
}
public static String methodTest2() {
Test test = new Test();
return test.testVariable;
}
public String methodTest3() {
return testVariable;
}
}
methodTest
This test will fail. You are attempting to get a non-static variable testVariable
from a static context methodTest()
.
此测试将失败。您正试图从静态上下文methodTest()获取非静态变量testVariable。
Accessed with:
String result = Test.methodTest();
methodTest2
This will work. You are making a new Test
class instance, and you can access the variable testVariable
normally.
这会奏效。您正在创建一个新的Test类实例,并且您可以正常访问变量testVariable。
Accessed with:
String result = Test.methodTest2();
methodTest3
This will work. You are accessing a non-static variable testVariable
from a non-static context methodTest3()
.
这会奏效。您正在从非静态上下文methodTest3()访问非静态变量testVariable。
Accessed with:
Test test = new Test();
String result = test.methodTest();
Resolution
Without seeing the rest of your code, it is hard to give you a suggestion for what to change. The code you supplied in your question is unlikely to be the culprit.
如果没有看到代码的其余部分,很难为您提供更改内容的建议。您在问题中提供的代码不太可能是罪魁祸首。
Edit
Your issue is that you are accessing the ContentValues
class statically...
您的问题是您正在静态访问ContentValues类...
ContentValues.put(COL_2,name);
You need to use the variable that you created: contentValues
.
您需要使用您创建的变量:contentValues。
#1
0
This error happens because you are mixing up static and non-static parts of your class.
发生此错误的原因是您混合了类的静态和非静态部分。
Take this example:
举个例子:
public class Test {
public String testVariable = "Honk";
public static String methodTest() {
return testVariable;
}
public static String methodTest2() {
Test test = new Test();
return test.testVariable;
}
public String methodTest3() {
return testVariable;
}
}
methodTest
This test will fail. You are attempting to get a non-static variable testVariable
from a static context methodTest()
.
此测试将失败。您正试图从静态上下文methodTest()获取非静态变量testVariable。
Accessed with:
String result = Test.methodTest();
methodTest2
This will work. You are making a new Test
class instance, and you can access the variable testVariable
normally.
这会奏效。您正在创建一个新的Test类实例,并且您可以正常访问变量testVariable。
Accessed with:
String result = Test.methodTest2();
methodTest3
This will work. You are accessing a non-static variable testVariable
from a non-static context methodTest3()
.
这会奏效。您正在从非静态上下文methodTest3()访问非静态变量testVariable。
Accessed with:
Test test = new Test();
String result = test.methodTest();
Resolution
Without seeing the rest of your code, it is hard to give you a suggestion for what to change. The code you supplied in your question is unlikely to be the culprit.
如果没有看到代码的其余部分,很难为您提供更改内容的建议。您在问题中提供的代码不太可能是罪魁祸首。
Edit
Your issue is that you are accessing the ContentValues
class statically...
您的问题是您正在静态访问ContentValues类...
ContentValues.put(COL_2,name);
You need to use the variable that you created: contentValues
.
您需要使用您创建的变量:contentValues。