Java 未定义的构造函数SQLite游标
我正在尝试为android上的SQlite数据库创建一个表。该表有5列:Int-id、URL-link、字符串标题、字符串描述和日期 为了构建我的代码,我一直使用和作为参考,但它们都没有显示如何向光标添加字符串以外的任何内容 这是我的密码:Java 未定义的构造函数SQLite游标,java,android,Java,Android,我正在尝试为android上的SQlite数据库创建一个表。该表有5列:Int-id、URL-link、字符串标题、字符串描述和日期 为了构建我的代码,我一直使用和作为参考,但它们都没有显示如何向光标添加字符串以外的任何内容 这是我的密码: public class Chapter { final SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); priv
public class Chapter {
final SimpleDateFormat FORMATTER =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
private String title;
private URL link;
private String description;
private Date date;
private int id;
//Constructor for table includes ID.
public Chapter(int id, URL link, String title, String description, Date date) {
this.id = id;
this.link = link;
this.title = title;
this.description = description;
this.date = date;
}
//Empty Constructor
public Chapter() {
// TODO Auto-generated constructor stub
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title.trim();
}
// getters and setters omitted for brevity
public URL getLink() {
return link;
}
public void setLink(String link) {
try {
this.link = new URL(link);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description.trim();
}
public String getDate() {
return FORMATTER.format(this.date);
}
public void setDate(String date) {
// pad the date if necessary
while (!date.endsWith("00")){
date += "0";
}
date = "";
try {
this.date = FORMATTER.parse(date.trim());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public Chapter copy(){
Chapter copy = new Chapter();
copy.title = title;
copy.link = link;
copy.description = description;
copy.date = date;
return copy;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Title: ");
sb.append(title);
sb.append('\n');
sb.append("Date: ");
sb.append(this.getDate());
sb.append('\n');
sb.append("Link: ");
sb.append(link);
sb.append('\n');
sb.append("Description: ");
sb.append(description);
return sb.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result + ((link == null) ? 0 : link.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Chapter other = (Chapter) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (link == null) {
if (other.link != null)
return false;
} else if (!link.equals(other.link))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
public int compareTo(Chapter another) {
if (another == null) return 1;
// sort descending, most recent first
return another.date.compareTo(date);
}
public int getId() {
// TODO Auto-generated method stub
return id;
}
public void setId(int parseInt) {
// TODO Auto-generated method stub
this.id = id;
}
public void setImage(String string) {
// TODO Auto-generated method stub
}
和我的助手类:
public class ChapterSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "chaptertable";
private static final int DATABASE_VERSION = 1;
// Database table
public static final String TABLE_CHAPTER = "Chapter";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_LINK = "link";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_PUBDATE = "pubdate";
public static final String COLUMN_IMAGEID = "imageid";
// Database creation SQL statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_CHAPTER
+ "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_LINK + " text not null, "
+ COLUMN_TITLE + " text not null,"
+ COLUMN_DESCRIPTION + " text not null"
+ COLUMN_PUBDATE + " text not null,"
+ COLUMN_IMAGEID + " text not null,"
+ ");";
public ChapterSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
// Method is called during an upgrade of the database,
// e.g. if you increase the database version
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CHAPTER);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Chapter chapter) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, chapter.getId()); // Chapter ID
values.put(COLUMN_LINK, chapter.getLink()); // Chapter Link
values.put(COLUMN_TITLE, chapter.getTitle()); // Chapter Title
values.put(COLUMN_DESCRIPTION, chapter.getDescription()); // Chapter Description
values.put(COLUMN_PUBDATE, chapter.getDate()); // Chapter Date
//values.put(COLUMN_IMAGEID, chapter.getImage()); // Chapter Image
// Inserting Row
db.insert(TABLE_CHAPTER, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Chapter getChapter(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CHAPTER, new String[] { COLUMN_ID,
COLUMN_ID, COLUMN_LINK }, COLUMN_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Chapter chapter = new Chapter(Integer.parseInt(cursor.getString(0)), cursor.getURL(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
// return contact
return chapter;
}
// Getting All Contacts
public List<Chapter> getAllContacts() {
List<Chapter> chapterList = new ArrayList<Chapter>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CHAPTER;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Chapter chapter = new Chapter();
chapter.setId(Integer.parseInt(cursor.getString(0)));
chapter.setLink(cursor.getURL(1));
chapter.setTitle(cursor.getString(2));
chapter.setDescription(cursor.getString(3));
chapter.setDate(cursor.getString(4));
//chapter.setImage(cursor.getString(5));
// Adding chapter to list
chapterList.add(chapter);
} while (cursor.moveToNext());
}
// return contact list
return chapterList;
}
// Updating single contact
public int updateContact(Chapter chapter) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, chapter.getId());
values.put(COLUMN_LINK, chapter.getLink());
values.put(COLUMN_TITLE, chapter.getTitle());
values.put(COLUMN_DESCRIPTION, chapter.getDescription());
values.put(COLUMN_PUBDATE, chapter.getDate());
//values.put(COLUMN_IMAGEID, chapter.getImage());
// updating row
return db.update(TABLE_CHAPTER, values, COLUMN_ID + " = ?",
new String[] { String.valueOf(chapter.getId()) });
}
// Deleting single contact
public void deleteContact(Chapter chapter) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CHAPTER, COLUMN_ID + " = ?",
new String[] { String.valueOf(chapter.getId()) });
db.close();
}
// Getting contacts Count
public int getChaptersCount() {
String countQuery = "SELECT * FROM " + TABLE_CHAPTER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public class ChapterSQLiteOpenHelper扩展了SQLiteOpenHelper{
私有静态最终字符串数据库\u NAME=“chaptertable”;
私有静态最终int数据库_VERSION=1;
//数据库表
公共静态最终字符串表\u CHAPTER=“CHAPTER”;
公共静态最终字符串列_ID=“_ID”;
公共静态最终字符串列\u LINK=“LINK”;
公共静态最终字符串列\u TITLE=“TITLE”;
公共静态最终字符串列\u DESCRIPTION=“DESCRIPTION”;
公共静态最终字符串列\u PUBDATE=“PUBDATE”;
公共静态最终字符串列\u IMAGEID=“IMAGEID”;
//数据库创建SQL语句
私有静态最终字符串数据库\u CREATE=“CREATE table”
+表11第章
+ "("
+列ID+“整数主键自动递增,”
+列链接+“文本不为空,”
+列标题+“文本不为空,”
+列描述+“文本不为空”
+列_PUBDATE+“文本不为空,”
+列_IMAGEID+“文本不为空,”
+ ");";
公共章节SqliteOpenHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//方法在创建数据库期间调用
@凌驾
public void onCreate(SQLiteDatabase){
execSQL(创建数据库);
}
//方法在数据库升级期间调用,
//例如,如果您增加数据库版本
//升级数据库
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//删除旧表(如果存在)
db.execSQL(“如果存在删除表”+表_章);
//再次创建表
onCreate(db);
}
/**
*所有CRUD(创建、读取、更新、删除)操作
*/
//添加新联系人
无效添加联系人(第章){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
values.put(COLUMN_ID,chapter.getId());//chapter ID
value.put(COLUMN_LINK,chapter.getLink());//chapter LINK
values.put(COLUMN_TITLE,chapter.getTitle());//chapter TITLE
values.put(COLUMN_DESCRIPTION,chapter.getDescription());//chapter DESCRIPTION
value.put(列_PUBDATE,chapter.getDate());//chapter Date
//value.put(列_IMAGEID,chapter.getImage());//chapter Image
//插入行
db.insert(表_章节,空,值);
db.close();//关闭数据库连接
}
//获得单一联系
章节getChapter(内部id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.query(TABLE_CHAPTER,新字符串[]{COLUMN_ID,
列_ID,列_LINK},列_ID+“=?”,
新字符串[]{String.valueOf(id)},null,null,null,null);
如果(光标!=null)
cursor.moveToFirst();
章节=新章节(Integer.parseInt(cursor.getString(0)),cursor.getURL(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
//回接
返回章节;
}
//获取所有联系人
公共列表getAllContacts(){
List chapterList=new ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+表格\章节;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
第章=新的一章();
chapter.setId(Integer.parseInt(cursor.getString(0));
第.setLink章(cursor.getURL(1));
第章.setTitle(cursor.getString(2));
第.setDescription章(cursor.getString(3));
第章.setDate(cursor.getString(4));
//第.setImage章(cursor.getString(5));
//向列表中添加章节
章节列表。添加(章节);
}while(cursor.moveToNext());
}
//返回联系人列表
返回章节列表;
}
//更新单个联系人
public int updateContact(第章){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(COLUMN_ID,chapter.getId());
value.put(COLUMN_LINK,chapter.getLink());
value.put(列标题,章节.getTitle());
value.put(COLUMN_DESCRIPTION,chapter.getDescription());
value.put(PUBDATE列,chapter.getDate());
//value.put(COLUMN_IMAGEID,chapter.getImage());
//更新行
返回db.update(表\u章节,值,列\u ID+“=?”,
新字符串[]{String.valueOf(chapter.getId())};
}
//删除单个联系人
公共联系人(第章){
SQLiteDatabase db=this.getWritableDatabase();
db.delete(表\u章节,列\u ID+“=?”,
新字符串[]{String.valueOf(chapter.getId())};
db.close();
}
//获取联系人计数
public int getChaptersCount(){
String countQuery=“SELECT*FROM”+表格\章节;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(countQuery,null);
cursor.close();
//返回计数
返回cursor.getCount();
}
我在values.put行中得到编译错误。put行用于链接,Chapter Chapter=new Chapter…行。您需要使用
copy()
方法中的setter方法设置属性,如:
public Chapter copy(){
Chapter copy = new Chapter();
copy.setTitle(this.title);
copy.setLink(this.link);
copy.setDescription(this.description);
copy.setDate(this.date);
return copy;
}
你需要